[英]Autolayout: Unable to simultaneously satisfy constraints
我正在嘗試使用自動布局和以下代碼在容器視圖中堆疊多個視圖:
UIView* prevView = commentBox; // Set the prev view to the container
for(Comment* comment in [[post info] comments]){
UIView* commentView = [[UIView alloc] initWithFrame:CGRectZero];
[commentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[commentBox addSubview:commentView];
// Vertically align with container view
[commentBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[commentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(commentView)]];
// Add a space of 8 between the previous view and the current view
[commentBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[prevView]-8-[commentView(==100)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(prevView, commentView)]];
// Store the current view for the next round
prevView = commentView;
}
// Finally, add a space between the last element and the container view
[commentBox addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[prevView]-8-[commentBox]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(prevView, commentBox)]];
commentBox
本身是自動放置在情節提要中的,這應該是安全的。
但是,當commentBox充滿子視圖時,它將顯示以下錯誤:
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:0x170097ca0 H:|-(0)-[UIView:0x170199e60] (Names: '|':UIView:0x17019a8f0 )>",
"<NSLayoutConstraint:0x170097cf0 H:[UIView:0x170199e60]-(0)-| (Names: '|':UIView:0x17019a8f0 )>",
"<NSLayoutConstraint:0x174099280 UIView:0x17419c8a0.width == UIView:0x17019a8f0.width>",
"<NSLayoutConstraint:0x17409a180 'UIView-Encapsulated-Layout-Width' H:[UIView:0x17419c8a0(320)]>",
"<NSLayoutConstraint:0x170098ec0 H:[UIView:0x170199e60]-(8)-[UIView:0x1701997e0]>",
"<NSLayoutConstraint:0x170098f10 H:[UIView:0x1701997e0(100)]>",
"<NSLayoutConstraint:0x170099050 H:[UIView:0x1701997e0]-(8)-[UIView:0x170199a50]>",
"<NSLayoutConstraint:0x1700990a0 H:[UIView:0x170199a50(100)]>",
"<NSLayoutConstraint:0x170099190 H:[UIView:0x170199a50]-(8)-[UIView:0x17019b110]>",
"<NSLayoutConstraint:0x1700991e0 H:[UIView:0x17019b110(100)]>",
"<NSLayoutConstraint:0x174099dc0 H:[UIView:0x17019b110]-(8)-[UIView:0x170199e60]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x174099dc0 H:[UIView:0x17019b110]-(8)-[UIView:0x170199e60]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
我不知道如何解決這個問題。
我將xCode 6.1與iOS SDK 8.1一起使用
先感謝您
我發現您的代碼中有兩件事是錯誤的。 首先,您不能在第一個水平約束或最后一個約束的[commentBox]中引用帶有[prevView]的超級視圖。 您需要使用豎線字符“ |”來引用它。 其次,您不能對整個視圖進行嚴格的約束(即我的意思是具有固定值的大小和空間)(除非這些數字完全正好等於超級視圖的寬度,即使如此,您也不應這樣做)它)。 因此,您要么需要允許寬度或間距靈活,要么不要將最后一個約束置於commentView的右側。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.