I want to add constraint programmatically and I use below code to add TOP and LEFT constraint.
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:label1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:110];
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:label1
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:10];
lable1
is added in my view controller. When I add this two constraint in the view like
[self.view addConstraint:top];
[self.view addConstraint:left];
It gives the error in the consol and constraint does not affect the lable.
2016-02-09 19:36:59.824 testinMRC[99160:313382] 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:0x7fa5c8738610 V:|-(110)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fa5c8628a70 'IB auto generated at build time for view with fixed frame' V:|-(88)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa5c8738610 V:|-(110)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>
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.
2016-02-09 19:36:59.889 testinMRC[99160:313382] 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:0x7fa5c87334b0 H:|-(10)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fa5c86285c0 'IB auto generated at build time for view with fixed frame' H:|-(188)-[UILabel:0x7fa5c8626940'Label'](LTR) (Names: '|':UIView:0x7fa5c86267b0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa5c87334b0 H:|-(10)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>
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.
Can anyone tell me why this error come? Help me to short out the solution
The problem is not your code, it is the
IB auto generated at build time for view with fixed frame
message:Interface Builder automatically generates constraints for you if you don't add them yourself. You could avoid it by either
Use Auto Layout
.or
remove at build time
.In your case error clearly states that you have conflicting constraints for your view. Probably because you a trying to add constraints to the view that already has some from Interface Builder. Even if you didn't set up any constrains explicitly IB does provide them for you when it detects that some are missing.
I see in comments you've mentioned that you want to do everything programmatically.In that case take a look at piece of code:
It's pretty self-explanatory. As you can see I had to add width and height constraints to the view, because only left and top doesn't fully describe it's position.
Result:
I encourage you to try out Visual Format Language. Same result can be achieved with much less code. This code leads to the very same result:
Let me know if this helped. Cheers.
It looks like you have conflicting layout constraints. A constraint was most likely set up in storyboard and you are trying to add a constraint programmatically that directly conflicts with it.
It looks like you are trying to move a UIView out of the way after an action occurs. I.E press a button and move a happy face picture from the middle of the screen to the top left corner.
Adding and removing constraints directly in code can be a HUGE headache with code bloat, unit testing and warnings filling up your console.
It might be a better idea to create the main UIView in storyboard and have a hidden placeholder UIView that holds all the constraints you want the UIView to have after you press the button. Then you can switch out all the constraints programmatically for those UIViews. So the view you want to modify will have the position, width and height of the placeholder and the place holder will have the position and height of the view you want to modify.
This way you will never have this error while changing the layout constraints programmatically because any errors will be caught by the storyboard.
There's some open source that does this for you in one or two lines of code. (There's some youtube video tutorials in the docs too) https://cocoapods.org/pods/SBP
After installing the cocoapod, your code would look something like this
this would be code inside the view controller with the UIView you want to modify and the placeholder UIView