What exactly does lowering the priority of a constraint do? - ios

I am constructing a variable size table view within another view. The table view should not scroll so I am programmatically determining its content size and adjusting a height constraint so that the table view always fits its content.
The problem I run into is a warning about a broken constraint:
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:0x170498b00 h=--& v=--& UIView:0x1024f2c90.height == 322 (active)>",
"<NSLayoutConstraint:0x17428d660 UITableView:0x103152400.height == 322 (active)>",
"<NSLayoutConstraint:0x174482da0 V:[UITableView:0x103152400]-(27)-| (active, names: '|':UIView:0x1024f2c90 )>",
"<NSLayoutConstraint:0x17429da10 V:|-(16)-[UITableView:0x103152400] (active, names: '|':UIView:0x1024f2c90 )>"
)
To fix this, I tried 2 things:
Set translatesAutoresizingMaskIntoConstraints = false. This causes everything to go haywire with the table view.
Lower the priority of the adjustable height constraint to 999.
Point 2 solves my problem and I'm able to adjust the height of the view using the lower priority constraint. But, I don't understand why this works.
So, how does Auto Layout interpret the priority of a constraint? I would've expected the NSAutoresizingMaskLayoutConstraint to take over the lower priority constraint and make it so I could not resize the view using the constraint.

To answer the question in the headline: Lowering the priority of a constraint tells the autolayout that the constraint is less important than all constraints with a higher priority.
This means that if two constraints are conflicting autolayout will use the one with the highest priority and disregard the other.
If two required constraints with identical priorities conflict, you will have an error message like the one you describe.

Since you are setting the height at run time I would select the height/vertical constraint in IB you don't need at run time and turn the 'Placeholder' for the constraint to 'Remove at build time', this should remove your warnings.
The priority setting determines when there are conflicting constraints which one will be used.

Related

Autolayouts warning,Unable to simultaneously satisfy constraints

Tableview is displaying in iphone5 ,but in other than iphone 5 i am getting this warning.could some one lemme know what is this warning all about?
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:0x60800008a820 UIImageView:0x7f8193515be0.height == 0.75*UITableViewCellContentView:0x7f81935153f0.height - 21 (active)>",
"<NSLayoutConstraint:0x60800008d2a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f81935153f0.height == 0.5 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60800008d2a0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f81935153f0.height == 0.5 (active)>
You have a constraint conflict, you could solve this by adding different priorities to your constraints, or deleting one of them.
You could also, give all the views you're using a restoration id, so in place of "UIImageView:0x7f8193515be0.height", it would appear 'id name'.height. This way you could see which view is having trouble with constraints.
You should study autolayout if you don't want to have these problems again...
AutoLayout guide
Firstly, try to change priorities for each height constraint in your table view cell to be at least 999.f
Obviously, you have an issue with height constraint.

Autolayout Issues in iOS 7 in Custom TableViewCell

I'm having issues with an Autolayout custom TableViewCell in iOS 7. The cell appears to display correctly, but I get a good deal of debugging console output, such as the following:
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:0x7f9c1a4b8500 V:[UILabel:0x7f9c1a4b72d0]-(0)-[UILabel:0x7f9c1a4b7680]>",
"<NSLayoutConstraint:0x7f9c1a4b8550 V:[UILabel:0x7f9c1a4b7680]-(0)-[UILabel:0x7f9c1a488910]>",
"<NSLayoutConstraint:0x7f9c1a4b85c0 V:[UILabel:0x7f9c1a488910]-(NSSpace(20))-| (Names: '|':UITableViewCellContentView:0x7f9c1a4b66d0 )>",
"<NSLayoutConstraint:0x7f9c1a4b87f0 V:|-(NSSpace(20))-[UILabel:0x7f9c1a4b6f00] (Names: '|':UITableViewCellContentView:0x7f9c1a4b66d0 )>",
"<NSLayoutConstraint:0x7f9c1a4b8840 V:[UILabel:0x7f9c1a4b6f00]-(NSSpace(8))-[UILabel:0x7f9c1a4b72d0]>",
"<NSAutoresizingMaskLayoutConstraint:0x7f9c1a4a70e0 h=--& v=--& V:[UITableViewCellContentView:0x7f9c1a4b66d0(44)]>"
)
I only get this output in iOS 7, and I see it on all of my custom table view cells, across multiple view controllers. I have followed all of the steps in this post:(Using Auto Layout in UITableView for dynamic cell layouts & variable row heights). I have tried adjusting the AutoresizingMask of the content view in these cells, but it does not stop these errors from appearing. I would greatly appreciate some advice on fixing these errors. Thanks!
The last one,
"NSAutoresizingMaskLayoutConstraint:0x7f9c1a4a70e0 h=--& v=--& V:[UITableViewCellContentView:0x7f9c1a4b66d0(44)]"
could indicate that in the tableviewcell the translatesAutoresizingMaskIntoConstraints property is set to YES. If this is the case try to set it to NO to avoid conflicts between your constraints and the automatic ones.
This means that you have constraints that conflict, and it is picking to satisfy one since they cannot all be satisfied.
Usually this means that you either have:
1)Constraints you know will not be satisfied simultaneously such as a minimum size or offset combined with a percentage or ratio. In this case all you need to do is reduce the priority of the one you want to break first.
Or, 2) duplicate constraints worded differently(center vertically, but also trailing space, etc) this one is harder for me to remotely diagnose, so posting the constraint would be important.
If the particular constraints are not that important to you (AKA you didn't spend that long adding them) then removing them all, adding suggested, and working from there is a very valid starting point.

Contraints greyed-out in Xcode

I am trying to debug+understand autolayout constraints and I notice that when debugging the view with xcode (using the cool layer thing) I noticed that on one element in the view the constraints look like this
and the view is indeed ignoring these constraints.
All constraints have the same priority (1000) since I want them all. All constraints were made with interface builder and not through code, and there are not warning or conflicts in IB.
But in runtime I do see this
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:0x79684f10 V:[UIImageView:0x79686800(>=160)]>",
"<NSLayoutConstraint:0x7968a310 V:[UIImageView:0x79686800]-(130.5)-| (Names: '|':UIView:0x79686790 )>",
"<NSLayoutConstraint:0x7968a340 V:|-(0)-[UIImageView:0x79686800] (Names: '|':UIView:0x79686790 )>",
"<NSLayoutConstraint:0x796997b0 'UIView-Encapsulated-Layout-Height' V:[CoverCell:0x79686570(192)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7969cd30 h=-&- v=-&- UIView:0x79686790.height == CoverCell:0x79686570.height>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x79684f10 V:[UIImageView:0x79686800(>=160)]>
So from this I understand that some rules conflict, but I'm not sure how to read this
the >=160 is a rule on the UIImageView so it would have height of atleast 160 and 130.5 is the bottom padding of the imageview (so when using systemLayoutSizeFittingSize:UILayoutFittingCompressedSize the height won't be 0. So the minimum height for the entire cell is 160+130.5)
The rest of the error I don't understand.
What is wrong with the constraints and why do constraint conflicts occur in runtime and not in IB?
XCode 6 now supports different layouts. This greyed out constraints exist in Compact Width | Any Height layout, for example, but you currently editing Any Width | Any Height.
More detailed:
Storyboard View Elements Greyed Out
It depends. If you don't use size classes the grey ones are the removed ones. And you need to remove them second time. No matter how stupid it sounds.
If you use size classes, it means that your current size class is different than for greyed constraint. (however the first scenario is also possible...)
In your case it looks like this is the first case. You need to remove greyed constraints once again.

Unable to simultaneously satisfy constraints. Leading and Trailing constraints conflicts

Can somebody help me understand these errors and and suggest how to go about solving them?
2014-09-19 08:52:45.812 Zazzle[23292:1834589] 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:0x7c0ba1e0 H:|-(8)-[UIView:0x7c0bb900] (Names: '|':UIView:0x7c0b6c20 )>",
"<NSLayoutConstraint:0x7c0ba210 H:[UIView:0x7c0bb900]-(8)-| (Names: '|':UIView:0x7c0b6c20 )>",
"<NSLayoutConstraint:0x7c0942e0 UIView:0x7c0b6c20.left == UIView:0x7c0bbfa0.left>",
"<NSLayoutConstraint:0x7c0c03e0 UIView:0x7c0b6c20.right == UIView:0x7c0bbfa0.right>",
"<NSLayoutConstraint:0x7c635250 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7c0bbfa0(2)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7c0ba210 H:[UIView:0x7c0bb900]-(8)-| (Names: '|':UIView:0x7c0b6c20 )>
fromFollow the below steps
Check the constraints of a UiObject and remove any constraints that duplicated, from size inspector of Xcode.
If there is no duplication,find out exact constraint that cause the problem
Try to change the priority of constraint from default required(1000) to high(750).
The conflict is right there: the first two constraints set the subview's leading and trailing values to be 8 pixels away from the containing view controller's left and right edges, while the third and fourth lines set them to be exactly even with the containing view's edges. Remove one pair or the other, or, as #dasdom said, lower the priorities of one of the constraint pairs.

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.

Resources