Jichao
Jichao

Reputation: 41845

ios autolayout how to write nslayoutconstriant correctly?

I know how to use interface builder to auto layout the button, for example. enter image description here

But when I try to use nslayoutconstraint,

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.thing1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeadingMargin multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.thing1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:[self topLayoutGuide] attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}

which I thought it should be the same with the constraints in interface builder.

but instead I got follow error message:

2015-12-10 22:59:01.320 Storyboard[7647:5116727] 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. 
(
    "<NSLayoutConstraint:0x12fd39a60 UIButton:0x12fd3c4f0'thing1'.leading == UIView:0x12fd3d590.leadingMargin>",
    "<NSIBPrototypingLayoutConstraint:0x12fe48040 'IB auto generated at build time for view with fixed frame' H:|-(20)-[UIButton:0x12fd3c4f0'thing1'](LTR)   (Names: '|':UIView:0x12fd3d590 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x12fd39a60 UIButton:0x12fd3c4f0'thing1'.leading == UIView:0x12fd3d590.leadingMargin>

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.
2015-12-10 22:59:01.327 Storyboard[7647:5116727] 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. 
(
    "<_UILayoutSupportConstraint:0x12fea5ac0 V:[_UILayoutGuide:0x12fd3d960(0)]>",
    "<_UILayoutSupportConstraint:0x12fe9e960 V:|-(0)-[_UILayoutGuide:0x12fd3d960]   (Names: '|':UIView:0x12fd3d590 )>",
    "<NSLayoutConstraint:0x12fd3d800 V:[_UILayoutGuide:0x12fd3d960]-(0)-[UIButton:0x12fd3c4f0'thing1']>",
    "<NSIBPrototypingLayoutConstraint:0x12fea5e40 'IB auto generated at build time for view with fixed frame' V:|-(20)-[UIButton:0x12fd3c4f0'thing1']   (Names: '|':UIView:0x12fd3d590 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x12fd3d800 V:[_UILayoutGuide:0x12fd3d960]-(0)-[UIButton:0x12fd3c4f0'thing1']>

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.

Why?

Upvotes: 0

Views: 70

Answers (1)

Nikolaj Simonsen
Nikolaj Simonsen

Reputation: 1670

Before adding any set of NSLayoutConstraint you should remember to set the view's translatesAutoresizingMaskIntoConstraints to NO or false.
I haven't worked with Objective-C for a while now, but if I'm not mistaken you can write something like:

[self.thing1 setTranslatesAutoresizingMaskIntoConstraints: NO]

or in Swift 2:

self.thing1.translatesAutoresizingMaskIntoConstraints = false

Also, you must always remember to add the self.thing1 to self.view before adding any constraints.

Upvotes: 2

Related Questions