简体   繁体   中英

Autolayout: Unable to simultaneously satisfy constraints

I'm trying to stack multiple views in a container view using autolayout and the following code:

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)]];

The commentBox itself is autolayouted in the storyboard, which should be safe.

However, when the commentBox gets filled with the subviews, it shows me the following error:

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.

I don't have any clue how to fix that.

I'm using xCode 6.1 with iOS SDK 8.1

Thank you in advance

I see two things wrong in your code. First, you can't refer to the superview with [prevView] where you make your first horizontal constraint, or [commentBox] in the last constraint. You need to refer to it with the pipe character, "|". Secondly, you can't make hard constraints (by that I mean sizes and spaces with fixed values) all the way across the view (unless those numbers all exactly add up to the width of the superview, and even then you shouldn't do it). So, you either need to allow the widths or spacing to be flexible, or don't make that last constraint to the right side of commentView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM