iOS Autolayout setting height constraint to zero leads to invalid layout
-
21-12-2019 - |
Question
I have following layout with AutoLayout
enabled:
UIView
UITableView
I have following constraints (only important here, rest are ommited):
|-(17)-[UITableView:0x15bfc200] priority 1000 (Top space to container)
[UITableView:0x15bfc200]-(17)-| priority 1000 (Bottom space to container)
[UITableView:0x15bfc200](=20) priority 750 (TableView height)
When I'm trying to set height to zero (ex, it has no content) app stops with exception:
Probably at least one of the constraints in the following list is one you don't want...blah blah blah...
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1559bf40 V:[UITableView:0x15bfc200]-(17)-| (Names: '|':UITableViewCellContentView:0x1559b330 )>
Currently, I set height using the following code:
heightConstraint.constant = 0;
What is the correct way to set tableView height to zero? Should I lower the priority of some constraint?
Solution
I think the problem is that the outer UIView is a specific size. Its height is 54 points, so when you initially set up your constraints they work. But when you change the height to 0, the math doesn't work: 17 + 0 + 17 != 54.
I'm not 100% sure what your goal for this small tableview is, but I think you should be able to position it with just a top constraint and a height constraint (no bottom constraint needed). This will remove the requirement that your constraints add up to the height of the superview.