Reputation: 739
Our application has a pattern where many screens have a number of UILabels on the top and a single UITableView bellow them which, when present, is always the last element on the page and expands right to the bottom of the screen. Because the pages are dependent on user specific data, we don't know upfront how many labels we need to display. Also, in some case when there is no data to display in UITableView, we simply want hide it.
Because of the dynamic nature of the page, I thought that wrapping all the UILabels and the UITableView in UIStackView and later manipulating the elements visibility (i.e. the isHidden property) in code is the way to go. I pinned all the corners of the UIStackView to the corners of the SuperView, except the bottom constraint, which I made greater than or equal of UIStackView bottom constraint. This was done in order to make sure that in case the UITableView is hidden, the UIStackView does not take the whole screen as it tends to display the labels in the middle of the screen when it does so. Basically if the UITableView is visible, I want the UIStackView to take the whole page, else, just the space it needs to display the labels.
The AutoLayout has a problem figuring out the height of the UITableView. I can think of two solutions to this:
Unlike the UITableView, the UILabels don't seem to have the same problem with height constraints as the UITableView. I'm not 100% sure why this is but my guess is that UILabels know in advance their height (probably via intrinsicContentSize()) but the UITableView does not until the run time?
Upvotes: 2
Views: 2673
Reputation: 341
You could combine your proposed solutions 1 & 2:
Set the StackView bottom constraint to be “equal” and then if it’s empty programmatically change it back to “greater or equal”.
You'll need to update constraints when your data loads to set the value depending on the data received.
Upvotes: 1