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

时间:2015-07-08 16:01:32

标签: ios objective-c uitableview nslayoutconstraint

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.

2 个答案:

答案 0 :(得分:3)

我猜是约束UIViewBottomHeight约束的优先级是必需的/ 1000 ..我会通过将优先级降低到999或其他东西来解除警告......

答案 1 :(得分:0)

听起来问题就出现了,因为你在底部的UIView上同时有一个高度约束和一个底部到容器约束。如果更改高度,则底部到容器约束关闭。您应该只能使用高度(或只是底部到容器)并完全摆脱底部到容器约束(或高度约束)。自动布局仅需要足够的约束来确定元素的唯一大小和位置。