2013-01-14 122 views
50

我所經歷和刪除每一個用戶約束但我仍然收到以下錯誤ONLY後我旋轉設備。但我絕對不知道爲什麼。有沒有人有任何想法?無法同時滿足約束條件 - 沒有約束到位

2013-01-14 21:30:31.363 myApp[35869:c07] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>", 
    "<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270)>", 
    "<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270)> 

Break on objc_exception_throw to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 
+0

你如何設置這個觀點 - 在代碼中,IB還是兩者的結合?你可以更新你的問題,包括? – jrturton

+0

可能的重複[獲取奇怪的錯誤談論約束在Xcode](http://stackoverflow.com/questions/11664115/getting-weird-error-talking-about-constraints-in-xcode) –

+0

關於約束問題的答案已清除是在這裏:[無法同時滿足約束,將嘗試通過打破約束恢復](http://stackoverflow.com/q/11664115/2725435) –

回答

89

讓我們一個一個來看看。

"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"

這話說視圖0xa330270()必須768點高。

"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270)>"

這話說視圖0xa331260()的底部邊緣必須是-1的間隙從底部,這是它的上海華。

"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270)>"

這是說,的頂邊必須是841點從它的父的頂部的間隙,

這三樣東西不能全是真實的 - 一個不能768點高,幷包含一個子視圖與頂部邊緣841點,從頂部嵌入且-1分從底部內側。你在哪裏定義了這些約束條件?

你還沒說你想達到什麼樣的佈局,但它看起來像你可能會對這是防止它在高度變化,當你旋轉設備的上海華盈的自動尺寸調整掩碼。據我所知,只有在以編程方式添加視圖時纔會出現自動調整約束,因爲故事板或xib可以是全自動佈局,也可以不是。除非你像添加一個自動佈局視圖(從nib加載?)到另一個非自動佈局nib視圖?

+2

原來的高度約束設置以及一個約束告訴它伸展到超級觀點。爲了解決這個問題,我剛剛擺脫了身高限制。謝謝! –

+3

我們如何擺脫高度限制?我面臨同樣的問題。 –

+2

是否有任何方法來告訴例如實際引用的視圖0xa331260? – thumbtackthief

9

我想這不是一個常見的錯誤,但我是個門外漢方式有所解決它。我收到了像上面那樣的神祕消息。爲了理解它,我創建了虛擬視圖類並將其附加到故事板中的視圖中。例如,如果我有一個UIView,我創建了一個名爲AddressView的類並將它附加到故事板中的這個視圖。它有點耗時,但它對我有用。之後,我不再使用object-id,而是獲得了類名,這幫助我輕鬆解決導致問題的視圖。我錯誤的消息現在讀,

2013-07-02 04:16:20.434 Myproject [2908:c07] Unable to simultaneously satisfy constraints. 
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x9edeae0 V:|-(0)-[AddressView:0x143ee020] (Names: '|':MainView:0x129eb6a0)>", 
    "<NSAutoresizingMaskLayoutConstraint:0x11e998c0 h=--& v=--& V:[MainView:0x129eb6a0(704)]>", 
    "<NSLayoutConstraint:0x156720b0 V:[AddressView:0x143ee020]-(896)-| (Names: '|':MainView:0x129eb6a0)>" 
) 

在這裏你可以看到,我的意見的MainView和地址視圖的名字引起的問題。

要解決這個問題,我只是提出我的子視圖(在這種情況下地址視圖)和重新回來。我認爲這個問題始於我在Xcode 4.5中使用新的Automatic Layour和舊技能或手動定位視圖。

不管怎麼說,不知道這是否是比勤奮更運氣,但無論如何,這可能是調試的方式不同。也許這有助於某人!

2

我已修正這個問題是刪除從XIB文件中的所有屬性translatesAutoresizingMaskIntoConstraints(開放XIB作爲源代碼)。

+0

也可以在代碼中設置self.dimmingView.translatesAutoresizingMaskIntoConstraints = NO – malhal

+1

這就是爲我解決的!在我的情況下,通過編程將'translatesAutoresizingMaskIntoConstraints'設置爲'YES'似乎可行,但我在控制檯'無法同時滿足約束'(儘管它看起來很好)。我以XML的形式打開,看到很多'translatesAutoresizingMaskIntoConstraints =「NO」'(我懷疑是故事板之間複製的結果,其中只有一個啓用了AutoLayout)。從XML中刪除之後 - 不再有警告! (我實際上甚至不需要以編程方式做任何事情,'YES'是默認值)。謝謝! –

11

感謝http://useYourLoaf.com這個完整的解決方案:

​​

快速的提示,我發現了埋在自動佈局一個WWDC 2015年會議的調試與約束問題的時候,可以幫助

如果您使用自動佈局時,您會熟悉Xcode在出現問題時吐出的日誌。爲了創建一個示例,我修改了my Stack View sample code併爲每個圖像添加了一個約束,使它們具有240的固定寬度(我們將會看到這不是一個好主意)。

enter image description here

,在普通寬度的觀點,如iPad的作品,但太寬了一個緊湊的寬度視圖,如iPhone的肖像。運行時的控制檯日誌閱讀起來並不好玩。跳過重複的文字你的問題的約束列表:

"<NSLayoutConstraint:0x7fc1ab520360 H:[UIImageView:0x7fc1ab532650(240)]>", 
"<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]>", 
"<NSLayoutConstraint:0x7fc1ab545cc0 UIView:0x7fc1ab53d870.trailingMargin == UIStackView:0x7fc1ab53dae0.trailing>", 
"<NSLayoutConstraint:0x7fc1ab545d10 UIStackView:0x7fc1ab53dae0.leading == UIView:0x7fc1ab53d870.leadingMargin>", 
"<NSLayoutConstraint:0x7fc1ab54e240 'UISV-alignment' UIStackView:0x7fc1ab53dc70.centerX == UIStackView:0x7fc1ab531a10.centerX>", 
"<NSLayoutConstraint:0x7fc1ab5167c0 'UISV-canvas-connection' UIStackView:0x7fc1ab531a10.leading == UIImageView:0x7fc1ab532650.leading>", 
"<NSLayoutConstraint:0x7fc1ab54ad80 'UISV-canvas-connection' H:[UIImageView:0x7fc1ab537380]-(0)-| (Names: '|':UIStackView:0x7fc1ab531a10)>", 
"<NSLayoutConstraint:0x7fc1ab5397d0 'UISV-canvas-connection' UIStackView:0x7fc1ab53dae0.leading == _UILayoutSpacer:0x7fc1ab54c3c0'UISV-alignment-spanner'.leading>", 
"<NSLayoutConstraint:0x7fc1ab54a4a0 'UISV-canvas-connection' UIStackView:0x7fc1ab53dae0.centerX == UIStackView:0x7fc1ab53dc70.centerX>", 
"<NSLayoutConstraint:0x7fc1ab54b110 'UISV-spacing' H:[UIImageView:0x7fc1ab532650]-(16)-[UIImageView:0x7fc1ab537380]>", 
"<NSLayoutConstraint:0x7fc1ab548210 'UISV-spanning-boundary' _UILayoutSpacer:0x7fc1ab54c3c0'UISV-alignment-spanner'.leading <= UIStackView:0x7fc1ab531a10.leading>", 
"<NSLayoutConstraint:0x7fc1ab551690 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fc1ab53d870(375)]>" 

然後,日誌會告訴你這上面的約束已經決定分手:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]> 

日誌輸出採用了自動佈局視覺格式語言,但很難從系統創建的那些中挑選出我的約束。堆棧視圖的情況尤其如此,其設計意圖是爲您創建大部分約束條件。在這個微不足道的例子中,我知道我剛剛添加的固定寬度約束破壞了事情,但很難從日誌中看到更復雜的視圖,它越難獲得。

添加標識符的約束

日誌變得輕鬆了很多,如果你添加一個標識符每個約束理解(NSLayoutConstraint以來的iOS 7已經有一個標識屬性)。在Interface Builder中找到的約束,並在屬性檢查器中添加的標識符(我使用$作爲前綴/後綴,使他們脫穎而出,在日誌中):

enter image description here

更新18月 - 2015年:正如在註釋中指出的那樣,標識符只能在Xcode 7開始的Interface Builder中編輯。它在Xcode 6.4中不可見。

如果添加約束代碼:

constraint.identifier = "$HeartImageFixedWidth$" 

這是棘手的,如果你正在使用的使用約束的陣列視覺格式的語言。例如,考慮銀行代碼片段來創建一個固定的寬度約束對心臟圖像視圖:

let heartWidth = NSLayoutConstraint.constraintsWithVisualFormat("[heart(240)]", 
       options:[], metrics:nil, views:viewsDictionary) 

由於heartWidth是式[NSLayoutConstraint]的陣列設置標識符是更多一點的工作:

for constraint in heartWidth { 
    constraint.identifier = "$HeartImageFixedWidth$" 
} 
heartImage.addConstraints(heartWidth) 

隨着我的限制,現在更容易找到他們在日誌文件中設置標識(見第4行):

"<NSLayoutConstraint:0x7f92a305aeb0 '$ContainerStackViewLeading$' UIStackView:0x7f92a3053220.leading == UIView:0x7f92a3052fb0.leadingMargin + 32>", 
"<NSLayoutConstraint:0x7f92a305b340 '$ContainerStackViewTrailing$' UIView:0x7f92a3052fb0.trailingMargin == UIStackView:0x7f92a3053220.trailing + 32>", 
"<NSLayoutConstraint:0x7f92a301cf20 '$HeartImageFixedWidth$' H:[UIImageView:0x7f92a3047ef0(240)]>", 
"<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]>", 
"<NSLayoutConstraint:0x7f92a3060cc0 'UISV-alignment' UIStackView:0x7f92a30533b0.centerX == UIStackView:0x7f92a30472b0.centerX>", 
"<NSLayoutConstraint:0x7f92a301c590 'UISV-canvas-connection' UIStackView:0x7f92a30472b0.leading == UIImageView:0x7f92a3047ef0.leading>", 
"<NSLayoutConstraint:0x7f92a305f680 'UISV-canvas-connection' H:[UIImageView:0x7f92a304d190]-(0)-| (Names: '|':UIStackView:0x7f92a30472b0)>", 
"<NSLayoutConstraint:0x7f92a3064190 'UISV-canvas-connection' UIStackView:0x7f92a3053220.leading == _UILayoutSpacer:0x7f92a30608a0'UISV-alignment-spanner'.leading>", 
"<NSLayoutConstraint:0x7f92a30415d0 'UISV-canvas-connection' UIStackView:0x7f92a3053220.centerX == UIStackView:0x7f92a30533b0.centerX>", 
"<NSLayoutConstraint:0x7f92a305fa10 'UISV-spacing' H:[UIImageView:0x7f92a3047ef0]-(16)-[UIImageView:0x7f92a304d190]>", 
"<NSLayoutConstraint:0x7f92a30508c0 'UISV-spanning-boundary' _UILayoutSpacer:0x7f92a30608a0'UISV-alignment-spanner'.leading <= UIStackView:0x7f92a30472b0.leading>", 
"<NSLayoutConstraint:0x7f92a3063240 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f92a3052fb0(375)]>" 

還多CLE arer其約束的系統選擇了突破:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]> 

添加標識符來限制也不是沒有努力,但它必須通過複雜的佈局的調試日誌進行排序下一次還清。

進一步閱讀

2

一個說明。如果您使用個人熱點連接進行測試,並且熱點狀態欄位於頂部,則會在日誌中看到此錯誤。它拋棄了限制。

希望這可以幫助別人..讓我瘋狂。

+1

嘿MTB,應該處理這個警告還是預期?我的應用程序設法看起來恰到好處,我不知道應該修復還是忽略它? –

3

YourConstraintView.translatesAutoresizingMaskIntoConstraints = NO;

爲我做。

3

它值得了解的基礎知識,並瞭解蘋果/ Xcode是試圖通過日誌來告訴你

H = Horizontal constraint(for leading and Trailing) 
V = Vertical constraint(top and bottom edge) 
h = height 
w = width 

TopEdge -> V:|-(points)-[VIEW:memoryAddress] 
BottomEdge -> V:[VIEW:memoryAddress]-(points)-| 
Leading -> H:|-(points)-[VIEW:memoryAddress] 
Trailing -> H:[VIEW:memoryAddress] -(points)-| 
height  -> h= --& v=--& V:[VIEW:memoryAddress((points)] 
width  -> VIEW:memoryAddress.width == points 
between -> H:[VIEW 1]-(51)-[VIEW 2] 

一旦你理解了這一點,讀你的特定錯誤是很容易

相關問題