chris P
chris P

Reputation: 6589

Changing NSLayoutConstraint constant in cellForRowAtIndexPath continually leads to "unable to satisfy constraints" error

I have numerous cells in my application like this. Some self sizing, others not.

For example, let's say we have a custom UITableViewCell. 100 height. Inside it's content view, we drag 2 UIView's. UIViewTop and UIViewBottom. Each 50 in height. UIViewTop we place at the top of the content view, and give it Leading/Trailing/Top/Height constraints. UIViewBottom we place at the bottom of the content view, and give it Leading/Trailing/Bottom/Height constraints. We also give Vertical Spacing constraint between UIViewTop and UIViewBottom.

Now, sometimes we want just UIViewTop to show, and other times we want UIViewTop AND UIViewBottom to show. So we drag out the UIViewBottomHeight Constraint as an IBOutlet NSLayoutConstraint.

Then, in cellForRowAtIndexPath... we either set this IBOutlet constraint to 0 or to 50, depending on whether we want to show that portion or not.

If we have self sizing cells we don't do anything. If we don't have self sizing cells, the cell's size is accurately determined in heightForRowAtIndexPath.

I'm getting the warning under both circumstances. In the debugger, it will happening immediately after the following the code...

`cell.constraintUIViewBottomHeight.constant = __`

I want to be able to change the constraint constants, but not receive the Unable to simultaneously satisfy constraints warning in the console.

Upvotes: 2

Views: 732

Answers (2)

Subbu
Subbu

Reputation: 2148

I'm guessing the priority for constraintUIViewBottomHeight constraint is Required/1000.. I'd give a shot by lowering the priority to 999 or something to get rid of the warning..

Upvotes: 3

trevorj
trevorj

Reputation: 2039

It sounds like the problem is arising because you have both a height constraint and a bottom to container constraint on the UIView at the bottom. If you change the height, the bottom to container constraint is off. You should be able to use just the height (or just the bottom to container) and get rid of the bottom to container constraint (or height constraint) altogether. Auto layout only needs enough constraints to determine the element's unique size and location.

Upvotes: 0

Related Questions