Autolayout unable to simultaneously satisfy constrains [duplicate] - ios

This question already has answers here:
Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint
(17 answers)
Closed 8 years ago.
I always create my views in code and so have never used auto layout (until now). The reason behind not using nibs is because I like the code better and it's simpler to maintain via GitHub etc, but enough about that.
I have a situation where I have three labels on the screen placed next to each other. Like so: |Label1| - some space - |Label2| - some space - |Label3|. Some space is let's say 15pts. Now what I would like to accomplish is that when I change the text of the labels I will call sizeToFit on them and would like that they would remain 10pts apart from each other. I know I could to this with a bit of math which is not even hard but I think that auto layout will be easier and more understandable and I will learn something new along the way. So what I did is:
Alloc & init all the labels
Give each label a frame (and set font, text color, etc)
Add the labels as a subview
Then I tried to set up the constraints between them like so:
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:#"[label1]-15-[label2]-15-[label3]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2, label3)];
[self addConstraints:constraints]; //Im subclassing an UIView that's why self
But I get an error saying:
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:0x14c14110 H:[UILabel:0x14c12ab0]-(15)-[UILabel:0x14c12f80]>",
"<NSAutoresizingMaskLayoutConstraint:0x14c08b00 h=--& v=--& UILabel:0x14c12ab0.midX == + 195>",
"<NSAutoresizingMaskLayoutConstraint:0x14c0dd90 h=--& v=--& H:[UILabel:0x14c12ab0(40)]>",
"<NSAutoresizingMaskLayoutConstraint:0x14c1c170 h=--& v=--& UILabel:0x14c12f80.midX == + 237.5>",
"<NSAutoresizingMaskLayoutConstraint:0x14c12f00 h=--& v=--& H:[UILabel:0x14c12f80(40)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x14c14110 H:[UILabel:0x14c12ab0]-(15)-[UILabel:0x14c12f80]>
Even if I try to do this just on the label 2 and 3 the same thing happens. I do not fully understand what the NSLog is saying.
Any help would be much appreciated. Also if you have any good links on where to "decode" those h=--& v=--& UILabel:0x14c12f80.midX == + 237.5 things would be awesome.

The h= and v= are the signatures of autoresizing masks being translated into constraints. Did you forget to set translatesAutoResizingMasksToConstraints = NO on one of your views?

Related

What layout constraint is this talking about?

I've read all the SO questions that are similar to this and I'm so lost. I'm getting the following error:
2015-09-14 22:59:40.455 guess-who[60143:9602686] 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:0x7c0f8e30 UIImageView:0x7b6efb60.top == _UILayoutGuide:0x7c0f67a0.top>",
"<_UILayoutSupportConstraint:0x7c0f8ae0 V:[_UILayoutGuide:0x7c0f67a0(0)]>",
"<_UILayoutSupportConstraint:0x7c0f0070 _UILayoutGuide:0x7c0f67a0.bottom == UIView:0x7c0f65e0.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x7b6f6130 h=--& v=--& UIImageView:0x7b6efb60.midY == + 204>",
"<NSAutoresizingMaskLayoutConstraint:0x7b6f6160 h=--& v=--& V:[UIImageView:0x7b6efb60(220)]>",
"<NSLayoutConstraint:0x7b6f6dc0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7c0f65e0(518)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7b6f6e20 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7c0f65e0] (Names: '|':UIView:0x7c0effc0 )>"
)
Will attempt to recover by breaking constraint
<_UILayoutSupportConstraint:0x7c0f0070 _UILayoutGuide:0x7c0f67a0.bottom == UIView:0x7c0f65e0.bottom>
I've gotten this for a bunch of other constraints which I've successfully solved, but this one is stumping me. I don't know what a UILayoutSupportConstraint is; the documentation is not very thorough. I've looked through the view debugger and UIView:0x7c0f65e0 seems to refer to my main view (although it's the child of a blank view, for some reason?). I'm not able to find anything with 0x7c0f67a0, although that seems to refer to the LayoutGuide, saying their bottoms must be equal. I'm not sure what other tools are available for me to use to figure this out.
EDIT:
Using the View Debugger, I've narrowed it down to one of these two constraints, neither of which I know the source of:
I can't find where either of these gets set. I know everyone's first suggestion is to set translatesAutoresizingMaskIntoConstraints to false, but that destroys my entire layout and I don't know how to fix it.
You could also consider adding accessibility identifiers and constraint identifiers to your views to make your AL logs more legible:
constraintVariableName.identifier = “constraintVariableName”;
In InterfaceBuilder, use the identifier property in the inspector.
self.loginButton.accessibilityLabel = NSLocalizedString("LoginButtonAccessibilityLabel", #"");
These id's will end up in the logs, such as you posted above, replacing things like UIView, UIImageView and UIConstraint with the ids.
It seems you have given constraints to image view with relation to Superview. So to satisfy you constraint of UILayoutGuide.bottom your constraints are broken. As you have added more or unnecessary constraints which were not required.
Constraints which are getting broken are:
UILayoutGuide.top = UIView.top
UILayoutGuide.height = 0
UILayoutGuide.bottom = UIView.bottom
UIImageView Height constraint
UIImageView Y position
UIView Height constraint
You have given UIView vertical spacing from top is '0' but missing bottom/height constraint.
Try changing the priority of height constraints from 1000 to 750 for UIImageview with height(220),UIView with height(518).
Also you need to check for bottom constraint for
"<NSAutoresizingMaskLayoutConstraint:0x7b6f6e20 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7c0f65e0] (Names: '|':UIView:0x7c0effc0 )>"
If it is possible then please attach demo on git for better idea.
Hope it helps.
Disabling translatesAutoresizingMaskIntoConstraints wrecks your layout because some of your stacked elements have no explicit height/width set (PlayAgain, cat, frame) and are based off the image contents.
Your example project isn't runnable out of the box, so I can't inspect the runtime layout. However, I'd start by specifying the height of those elements so there are fewer auto constraints. It also looks like you're changing a lot of the constraints at runtime, so you need to be cautious about altering constraints that will conflict with each other. Make sure that the layout's happy before you start constraint-constant fiddling!
Use this following Line. It may be help you.
[view setTranslatesAutoresizingMaskIntoConstraints:NO];

"Unable to simultaneously satisfy constraints" when AutoLayout is disabled

I have disabled Auto Layout in the interface builder for a specific view:
But I get this error in the console, when I load the ViewController for the xib file:
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)
I am working with frames in this view and I usually don't get any Auto Layout errors when I have disabled it in the Interface Builder. This is the constraints that seems to break:
(
"<NSLayoutConstraint:0x170f5cd0 V:[UIView:0x1709e560]-(0)-[UITableView:0x162e2200]>",
"<NSAutoresizingMaskLayoutConstraint:0x17185130 h=--& v=--& UIView:0x1709e560.midY == + 48>",
"<NSAutoresizingMaskLayoutConstraint:0x157e29a0 h=--& v=--& V:[UIView:0x1709e560(96)]>",
"<NSAutoresizingMaskLayoutConstraint:0x171c2670 h=--& v=--& TableHeaderView:0x1705d1c0.midY == + 72.5>",
"<NSAutoresizingMaskLayoutConstraint:0x171cb510 h=--& v=--& V:[TableHeaderView:0x1705d1c0(145)]>",
"<NSAutoresizingMaskLayoutConstraint:0x1573fca0 h=-&- v=-&- UITableView:0x162e2200.midY == UIView:0x17505080.midY + 72.5>",
"<NSAutoresizingMaskLayoutConstraint:0x1573fcd0 h=-&- v=-&- UITableView:0x162e2200.height == UIView:0x17505080.height - 145>",
"<NSAutoresizingMaskLayoutConstraint:0x171e4450 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x17505080] (Names: '|':UIViewControllerWrapperView:0x17034d00 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x16c9ad30 V:[UIView:0x16cc2a00]-(0)-[UITableView:0x15249400]>
Correct me if I am wrong, but I only thought I would be getting this error if Auto Layout was enabled?
To me it seems like it thinks I have setup a constraint between the UIView and the UITableView, but I havn't! Auto Layout is disabled in the Interface Builder and I do not set any constraints in the code.
I have tried setting translatesAutoresizingMaskIntoConstraints to NO, but my frames are not respected when I do this.
It seems to me, like the view is working with Auto Layout. I have tried to delete the file and construct it again, but I still get the same error.
Why am I getting this error when Auto Layout is disabled in the xib file?
EDIT:
It turns out that I had made a mistake when I was searching for constraints. I found this line in my code:
NSDictionary* views = #{#"searchHeaderView": self.searchHeader.view, #"tableView": self.tableView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[searchHeaderView]-(==0)-[tableView]" options:0 metrics:nil views:views]];
I think that explains it :-P
Something weird is going on, because if you look closely, the memory addresses of at the 'will break' constraint and views attached to it are not in the array, so these are different instances.
Regardless, the unwelcome Auto Layout behavior is consistent with your suspicion: If one constraint gets added to the view hierarchy, AutoLayout "turns back on" at least for for that part of it and you get a mess of autoresizingmask constraints.
So this is a debugging issue. Some commands methods you can use in the debugger:
po [[UIWindow keyWindow] recursiveDescription]
Gives you a complete outline of the view hierarchy, with UIView descriptions, which include frames.
po [[UIWindow keyWindow] _autolayoutTrace]
This is private API but that's not an issue during development and especially not debugging. This actually works best if you follow the suggestion and add a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints (the breakpoints navigator, at the bottom). Best of luck.
I am pretty sure that if you do not want to use AutoLayout then you need to set translatesAutoresizingMaskIntoConstraints to YES rather than NO.
This then ensures that constraints are created for you which reflect the position drawn in interface builder without you having to add constraints. Perhaps you have some constraints assigned which clash with those it thinks it needs to autogenerate? I would remove any constraints, if you can find any. If there are none to delete, can you post the list it says are clashing?

iOS: Unable to simultaneously satisfy constraints

I've seen the common response to this question of not having constraints set up properly. However, I've worked through all of the ambiguities in Interface Builder, and all constraints seem OK. I'm not sure what to check next. I can't even tell where it is coming from. I'm not setting any constraints in code.
What can I do to figure out where it is coming form?
What can be the cause if Interface Builder doesn't report any ambiguities?
Here's the specific error:
2014-06-28 19:54:36.070 Prayer Feed[4497:60b] 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:0x178290360 H:[UIImageView:0x13ee32490(20)]>",
"<NSLayoutConstraint:0x1782904f0 UITableViewCellContentView:0x178166840.centerX == UIImageView:0x13ee32490.centerX + 62>",
"<NSLayoutConstraint:0x178290540 H:|-(52)-[UIImageView:0x13ee32490] (Names: '|':UITableViewCellContentView:0x178166840 )>",
"<NSAutoresizingMaskLayoutConstraint:0x170286d60 h=--& v=--& H:[UITableViewCellContentView:0x178166840(247)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x178290360 H:[UIImageView:0x13ee32490(20)]>
I know the two place in code where I have a UIImageView constrained to 20x20, but again, IB tells me everything is fine.
This is the part where the problem is:
<NSAutoresizingMaskLayoutConstraint:0x170286d60 h=--& v=--& H:[UITableViewCellContentView:0x178166840(247)]>
This mask needs to be turned off.
I think that mask is off by default for IB elements that have constraints. So I assume you made something in code. You can turn it off in code like this:
contentView.translatesAutoresizingMaskIntoConstraints = NO
If this is really all IB only then I would like to know what code you use around creating table cells.

Unable to simultaneously satisfy constraints when animating

I have six user constraints setup with IB that look like this:
H:|-(593)-[UIView(411)]-(20)-|
V:|-(20)-[UIView(288)]-(396)-|
I grow and shrink the view by changing the constraints and then calling layoutIfNeeded. For example, to grow the view I will do:
H:|-(20)-[UIView(984)]-(20)-|
V:|-(20)-[UIView(663)]-(20)-|
And then call
[UIView animateWithDuration:.5 animations:^{
[self.view layoutIfNeeded];
}];
This technique grows and shrinks my view, and it looks nice, but I'm given a rather confusing warning:
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:0x148d5af0 H:[UIView:0x148d4e50(411)]>",
"<NSLayoutConstraint:0x148cc940 H:[UITableView:0xace7600(319)]>",
"<NSLayoutConstraint:0x148ce040 H:|-(NSSpace(20))-[UITableView:0xacd4e00] (Names: '|':UIView:0x148cddd0 )>",
"<NSLayoutConstraint:0x148cdf00 H:[UITableView:0xace7600]-(NSSpace(20))-| (Names: '|':UIView:0x148cddd0 )>",
"<NSLayoutConstraint:0x148cdea0 H:[UITableView:0xacd4e00]-(NSSpace(8))-[UITableView:0xace7600]>",
"<NSLayoutConstraint:0x148d4c10 UIView:0x148cddd0.trailing == UIView:0x148cdd40.trailing>",
"<NSLayoutConstraint:0x148d4b90 H:|-(0)-[UIView:0x148cddd0] (Names: '|':UIView:0x148cdd40 )>",
"<NSLayoutConstraint:0x148d6020 H:|-(320)-[UIView:0x148cdd40] (Names: '|':UIView:0x148cd330 )>",
"<NSLayoutConstraint:0x148d5fa0 UIView:0x148cdd40.trailing == UIView:0x148cd330.trailing>",
"<NSLayoutConstraint:0x148d5f60 H:[UIView:0x148d4e50]-(NSSpace(20))-| (Names: '|':UIView:0x148cd330 )>",
"<NSLayoutConstraint:0x148d5ee0 H:|-(20)-[UIView:0x148d4e50] (Names: '|':UIView:0x148cd330 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x148cc940 H:[UITableView:0xace7600(319)]>
Now all of these constraints are generated by IB. I've double^(triple!) checked this. I've put these constraints together with a pen and paper and have gotten this:
UIView_A H:[-(20)-[UIView_E]-(20)-] and H:[-(320)-(UIView_B)
UIView_B H:[-(0)-[UIView_D]
UIView_C H:[UIView_C(411)]
UIView_D H:[-(20)-[UITableView_F]-[UITableView_G(319)]-(20)-]
I don't understand how these constraints can't be satisfied. They look fine. I don't change them and they're generated by IB. Aren't constraints generated by IB automatically satisfiable?
Or, at a minimum, is there a way to stop warnings? It behaves perfectly and I don't need to see that it's breaking a constraint that doesn't seem to do anything anyway.
This constraint:
H:[UITableView:0xace7600(319)]>"
seems to be an impediment for the system resolution.
Can you remove it ?
So, it turns out, the order at which I change constraints matters.
To grow the view, I will
Increase the width: H:|-(593)-[UIView(984)]-(20)-|
Decrease the leading space: H:|-(20)-[UIView(984)]-(20)-|
This produces no warnings. However, if I do this in the opposite order, I'll get a warning:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints ...
...Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1567f650 H:[UITableView:0x119e6200(319)]>
When shrinking the view, I copied the code that increased the width (with the same order) and just changed the values. That gave me the warning I posted in my original question. When I switched the order for shrinking, the warnings disappeared.
Why is this the case? I don't know. I'll update as I discover more.
I got same message, and I finally got it why it happens,
My solution is: Do not let any object FLIP OVER during at any moment of the animation.
In other words, contraints are should be OUTside of any object,
but sometimes constraints are inside during animation, not like our expection.
In another words, do not let top margin invade bottom margin due to constraint animation.
For example,
top constraint: topA = initially 100
[Box A]
bottom constraint: botA = initially 150
now, if you set like below and animate,
topA = 300
botA = 25
then error should occur,
why: thread invades the bottom margin BEFORE bottom margin go down.
So,
You'd rather change the ORDER,
botA = 25
topA = 300
Then error will disappear because bottom constraint would preserve the height of the mass and next top constraint would shrink the object's height not invading the bottom margin.
*Point:
Let the object have width and height greater than 0 CONTINUALLY even during animation,
not interrupted by constraints change.
I hope I helped you.

Unable to simultaneously satisfy constraints - No constraints in place

I have gone through and removed every single user constraint yet I am still getting the following error ONLY after I rotate the device. I have absolutely no clue why though. Does anyone have any ideas?
2013-01-14 21:30:31.363 myApp[35869:c07] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>",
"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>",
"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
Let's look at these one by one.
"<NSAutoresizingMaskLayoutConstraint:0x84543d0 h=--& v=--& V:[UIView:0xa330270(768)]>"
This is saying view 0xa330270 (A) must be 768 points high.
"<NSLayoutConstraint:0xa338350 V:[UIView:0xa331260]-(-1)-| (Names: '|':UIView:0xa330270 )>"
This is saying view 0xa331260 (B)'s bottom edge must be a gap of -1 from the bottom of A, which is it's superview.
"<NSLayoutConstraint:0xa338390 V:|-(841)-[UIView:0xa331260] (Names: '|':UIView:0xa330270 )>"
This is saying that B's top edge must be a gap of 841 points from the top of its superview, A.
These three things can't all be true - A can't be 768 points high, and contain a subview with a top edge 841 points inset from the top and -1 points inset from the bottom. Where have you defined each of these constraints?
You haven't said what layout you are trying to achieve, but it looks like you might have an autoresizing mask on the superview that is preventing it changing in height when you rotate the device. As far as I know the autoresizing constraints only appear if you have added views programmatically, since a storyboard or xib is either all-autolayout, or not. Unless you are doing something like adding an auto laid out view (loaded from a nib?) to another view from a non-autolayout nib?
Its worth knowing the basics, and understand what Apple/Xcode is trying to tell you through the logs
H = Horizontal constraint(for leading and Trailing)
V = Vertical constraint(top and bottom edge)
h = height
w = width
TopEdge -> V:|-(points)-[VIEW:memoryAddress]
BottomEdge -> V:[VIEW:memoryAddress]-(points)-|
Leading -> H:|-(points)-[VIEW:memoryAddress]
Trailing -> H:[VIEW:memoryAddress] -(points)-|
height -> h= --& v=--& V:[VIEW:memoryAddress((points)]
width -> VIEW:memoryAddress.width == points
between -> H:[VIEW 1]-(51)-[VIEW 2]
Once you understand this, reading your specific error is pretty easy
thanks to http://useYourLoaf.com for this complete solution:
http://useyourloaf.com/blog/using-identifiers-to-debug-autolayout.html
A quick tip I found buried in a WWDC 2015 session on Auto Layout that helps when debugging problems with constraints
If you have used Auto Layout you will be familiar with the log that Xcode spits out when you get something wrong. To create an example I modified my Stack View sample code and added a constraint to each of the images to give them a fixed width of 240 (not a good idea as we will see).
That works in regular width views such as the iPad but is too wide for a compact width view such as the iPhone in portrait. The console log at runtime is not fun to read. Skipping the boilerplate text you get a list of the problematic constraints:
"<NSLayoutConstraint:0x7fc1ab520360 H:[UIImageView:0x7fc1ab532650(240)]>",
"<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]>",
"<NSLayoutConstraint:0x7fc1ab545cc0 UIView:0x7fc1ab53d870.trailingMargin == UIStackView:0x7fc1ab53dae0.trailing>",
"<NSLayoutConstraint:0x7fc1ab545d10 UIStackView:0x7fc1ab53dae0.leading == UIView:0x7fc1ab53d870.leadingMargin>",
"<NSLayoutConstraint:0x7fc1ab54e240 'UISV-alignment' UIStackView:0x7fc1ab53dc70.centerX == UIStackView:0x7fc1ab531a10.centerX>",
"<NSLayoutConstraint:0x7fc1ab5167c0 'UISV-canvas-connection' UIStackView:0x7fc1ab531a10.leading == UIImageView:0x7fc1ab532650.leading>",
"<NSLayoutConstraint:0x7fc1ab54ad80 'UISV-canvas-connection' H:[UIImageView:0x7fc1ab537380]-(0)-| (Names: '|':UIStackView:0x7fc1ab531a10 )>",
"<NSLayoutConstraint:0x7fc1ab5397d0 'UISV-canvas-connection' UIStackView:0x7fc1ab53dae0.leading == _UILayoutSpacer:0x7fc1ab54c3c0'UISV-alignment-spanner'.leading>",
"<NSLayoutConstraint:0x7fc1ab54a4a0 'UISV-canvas-connection' UIStackView:0x7fc1ab53dae0.centerX == UIStackView:0x7fc1ab53dc70.centerX>",
"<NSLayoutConstraint:0x7fc1ab54b110 'UISV-spacing' H:[UIImageView:0x7fc1ab532650]-(16)-[UIImageView:0x7fc1ab537380]>",
"<NSLayoutConstraint:0x7fc1ab548210 'UISV-spanning-boundary' _UILayoutSpacer:0x7fc1ab54c3c0'UISV-alignment-spanner'.leading <= UIStackView:0x7fc1ab531a10.leading>",
"<NSLayoutConstraint:0x7fc1ab551690 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fc1ab53d870(375)]>"
The log then tells you which of the above constraints it has decided to break:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fc1ab536ef0 H:[UIImageView:0x7fc1ab537380(240)]>
The log output uses the auto layout visual format language but it is hard to pick out my constraints from those created by the system. This is especially the case with stack views which are by design intended to create most of the constraints for you. In this trivial example I know the fixed width constraints that I just added broke things but it is hard to see that from the log and the more complex the view the harder it gets.
Adding an Identifier to a Constraint
The log gets a lot easier to understand if you add an identifier to each constraint (NSLayoutConstraint has had an identifier property since iOS 7). In Interface Builder find the constraint and add the identifier in the Attributes inspector (I am using $ as a prefix/suffix to make them stand out in the log):
Update 18-August-2015: As pointed out in the comments the identifier can only be edited in Interface Builder starting with Xcode 7. It is not visible in Xcode 6.4.
If adding the constraint in code:
constraint.identifier = "$HeartImageFixedWidth$"
It is trickier if you are using the visual format language which uses arrays of constraints. For example, consider the Swift code fragment to create a fixed width constraint for the heart image view:
let heartWidth = NSLayoutConstraint.constraintsWithVisualFormat("[heart(240)]",
options:[], metrics:nil, views:viewsDictionary)
Since heartWidth is an array of type [NSLayoutConstraint] setting the identifier is a little more work:
for constraint in heartWidth {
constraint.identifier = "$HeartImageFixedWidth$"
}
heartImage.addConstraints(heartWidth)
With identifies set for my constraints it is now much easier to find them in the log file (see the first four lines):
"<NSLayoutConstraint:0x7f92a305aeb0 '$ContainerStackViewLeading$' UIStackView:0x7f92a3053220.leading == UIView:0x7f92a3052fb0.leadingMargin + 32>",
"<NSLayoutConstraint:0x7f92a305b340 '$ContainerStackViewTrailing$' UIView:0x7f92a3052fb0.trailingMargin == UIStackView:0x7f92a3053220.trailing + 32>",
"<NSLayoutConstraint:0x7f92a301cf20 '$HeartImageFixedWidth$' H:[UIImageView:0x7f92a3047ef0(240)]>",
"<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]>",
"<NSLayoutConstraint:0x7f92a3060cc0 'UISV-alignment' UIStackView:0x7f92a30533b0.centerX == UIStackView:0x7f92a30472b0.centerX>",
"<NSLayoutConstraint:0x7f92a301c590 'UISV-canvas-connection' UIStackView:0x7f92a30472b0.leading == UIImageView:0x7f92a3047ef0.leading>",
"<NSLayoutConstraint:0x7f92a305f680 'UISV-canvas-connection' H:[UIImageView:0x7f92a304d190]-(0)-| (Names: '|':UIStackView:0x7f92a30472b0 )>",
"<NSLayoutConstraint:0x7f92a3064190 'UISV-canvas-connection' UIStackView:0x7f92a3053220.leading == _UILayoutSpacer:0x7f92a30608a0'UISV-alignment-spanner'.leading>",
"<NSLayoutConstraint:0x7f92a30415d0 'UISV-canvas-connection' UIStackView:0x7f92a3053220.centerX == UIStackView:0x7f92a30533b0.centerX>",
"<NSLayoutConstraint:0x7f92a305fa10 'UISV-spacing' H:[UIImageView:0x7f92a3047ef0]-(16)-[UIImageView:0x7f92a304d190]>",
"<NSLayoutConstraint:0x7f92a30508c0 'UISV-spanning-boundary' _UILayoutSpacer:0x7f92a30608a0'UISV-alignment-spanner'.leading <= UIStackView:0x7f92a30472b0.leading>",
"<NSLayoutConstraint:0x7f92a3063240 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f92a3052fb0(375)]>"
It also much clearer which of the constraints the system has chosen to break:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f92a3009be0 '$StarImageFixedWidth$' H:[UIImageView:0x7f92a304d190(240)]>
Adding identifiers to constraints is not without effort but it can pay off the next time you have to sort through the debug log of a complex layout.
Further Reading
WWDC 2015 Session 219 Mysteries of Auto Layout, Part 2
I guess this is not a common error, but I solved it somewhat in a layman way. I was getting cryptic messages like the one above. To make sense of it, I created dummy view classes and attached it to the views in my storyboard. For example, if I had a UIView, I created a class called AddressView and attached it to this view in story board. Its a bit time consuming, but it worked for me. After that instead of object-ids, I got class names which helped me zero in on the views that were causing the issue very easily. My error message now read,
2013-07-02 04:16:20.434 Myproject [2908:c07] 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:0x9edeae0 V:|-(0)-[AddressView:0x143ee020] (Names: '|':MainView:0x129eb6a0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x11e998c0 h=--& v=--& V:[MainView:0x129eb6a0(704)]>",
"<NSLayoutConstraint:0x156720b0 V:[AddressView:0x143ee020]-(896)-| (Names: '|':MainView:0x129eb6a0 )>"
)
Here you can see, the names of my views MainView and Address view are causing the issue.
To resolve it, I just moved my subview (in this case Address view) and repositioned it back. I think the issue began as I was using a mix of new Automatic Layour in Xcode 4.5 and old skills or manually positioning the views.
Anyways, not sure if it was more luck than diligence, but nevertheless this could be a different way of debugging. Maybe this helps someone!
YourConstraintView.translatesAutoresizingMaskIntoConstraints = NO;
Did it for me.
I've fixed this problem be deleting all translatesAutoresizingMaskIntoConstraints properties from xib file (Open xib as a source code).
One note. You get this error in logs if you are testing using a personal hotspot connection, and the hotspot status bar is at the top. It throws off the constraints.
Hope this helps someone.. was driving me nuts.
For me this error was spitted when I gave tableView.estimatedRowHeight = UITableViewAutomaticDimension
This should have been tableView.estimatedRowHeight = "Some hardcoded value"
I had this problem and took me 2 days to figure out the source of the problem....
If you open a storyboard programmatically in you code just make sure you do it like this:
UIStoryboard *story = [UIStoryboard storyboardWithName:#"MovieMaker" bundle:nil];
UIViewController *vc = [story instantiateInitialViewController];
//this causes layout to break [self presentViewController:vc animated:YES completion:nil];
[self showViewController:vc sender:nil];
I was using the commented line (using presentViewController) and the orientation bug has happening throwing constraints conflicts that weren't my constraints... changing to showViewController all constraints conflicts were gone and orientation works...... (I don't really know why it works with show and not present... still thinking it's... ALIENS...)
This issue of the generated message "Unable to simultaneously satisfy contraints" in the debug console, is also experienced in XCode 9.4.
In my particular instance on the iPad simulator, the message would generate:
1) Only when placing the focus on a particular UITextField.
2) Even with all view contraints removed.
2) Even with all view contraints "Reset to Suggested Contraints".
However, when the software keyboard was toggled on to display, the message would not be generated.
Therefore, how much time should I spend on this issue, that in my instance is only generated when the software keyboard is toggled off.
this line solved my problem when logs like above in uitableviewCell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension > CellHeight.rowHeight44 ? UITableView.automaticDimension : CellHeight.rowHeight44
}

Resources