jakedunc
jakedunc

Reputation: 982

UITableView inside UIView with 20px Top Space constraint

I have a "Unable to simultaneously satisfy constraints." in my iOS app log that I cannot understand or fix.

I have a bunch of UIViews going vertically down my UIViewController. Each of them have a set height, in this case 120px, with a Top Space constraint to the view above it and a Bottom Space constraint to the view below it.

enter image description here

Then inside the view there is a UITableView with a 20px Top Space constraint. That means when the height of the view changes the table view will get bigger but still have its 20px top space.

enter image description here

This works perfectly and gives no warning at the Storyboard level, but gives the following error to the console:

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. ( "< NSLayoutConstraint:0x7f939bf47b30 V:[UIView:0x7f939bf5ad20(0)]>", "< NSLayoutConstraint:0x7f939bf47bd0 V:|-(20)-[TabItemsTableView:0x7f939a944400] (Names: '|':UIView:0x7f939bf5ad20 )>", "< NSLayoutConstraint:0x7f939bf47c20 V:[TabItemsTableView:0x7f939a944400]-(0)-| (Names: '|':UIView:0x7f939bf5ad20 )>" )

Will attempt to recover by breaking constraint < NSLayoutConstraint:0x7f939bf47c20 V:[TabItemsTableView:0x7f939a944400]-(0)-| (Names: '|':UIView:0x7f939bf5ad20 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

The 20px Top Space is to give the effect of a separator that only separates the views if their table views have rows, otherwise their height is set to 0 and the whole view disappears. If the 20px Top Space was on the parent view then when the height is 0 the 20px would persist.

Upvotes: 0

Views: 446

Answers (1)

jakedunc
jakedunc

Reputation: 982

OK I have solved this problem. It was happening because I had the top margin of 20px and then I was setting the parent view to a height of 0, so it didn't know whether to listen to the top margin of 20 or bottom margin of 0 constraints for a view with height of 0.

To fix this issue I set the priority of my top 20px constraint to 999 (when the default priority is 1000) so that this constraint is ignored when a choice needs to be made between the two.

Upvotes: 0

Related Questions