【问题标题】:Autolayout: Unable to simultaneously satisfy constraints自动布局:无法同时满足约束
【发布时间】:2014-12-16 22:15:02
【问题描述】:

我正在尝试使用自动布局和以下代码在容器视图中堆叠多个视图:

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 本身在情节提要中自动布局,应该是安全的。

但是,当评论框被子视图填满时,它会显示以下错误:

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 setTranslatesAutoresizingMaskIntoConstraints:NO];不是commentView - 重要的是父母。

标签: ios objective-c xcode autolayout constraints


【解决方案1】:

我发现您的代码中有两处错误。首先,您不能在创建第一个水平约束的地方使用 [prevView] 或在最后一个约束中使用 [commentBox] 来引用超级视图。您需要使用竖线字符“|”来引用它。其次,您不能在整个视图中一直进行硬约束(我的意思是大小和具有固定值的空间)(除非这些数字完全加起来就是超级视图的宽度,即使那样你也不应该这样做它)。因此,您要么需要允许宽度或间距灵活,要么不要将最后一个约束设置在 commentView 的右侧。

【讨论】:

  • 非常感谢!这是我第一次按代码自动布局视图并将 V 和 H 混合在一起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多