Search code examples
iosuitableviewautolayoutuitableviewautomaticdimension

Table View cell height with complicated structure and UITableViewAutomaticDimension


How to create automatic table view cell size with complicated structure, which contains big amount of subview?

Here is an example of table view cell:Table view cell example

For example, total height of table view cell is 220 px: Brown area - 70 px, green area 90px, red - 10 px and black - 50px. Blue area always will be same height as tableview cell.

How can i decrease tableview cell height using autolayout and UITableViewAutomaticDimension to become 130px by removing/hiding/whatever that whole green area?

Is it possible to achieve it using UITableViewAutomaticDimension and correct constraints without any old style approach?

NOTE: Minimum version is iOS 8(but iOS 9+ solution will be ok too, i'll increase minimum version...)


Solution

  • I would suggest using a vertical UIStackView with everything else inside it. A stack view has the wonderful feature that it sets up the constraints of its contained views for you — and it compensates when you insert or remove a contained view, which seems to be what you're after. You could set up those constraints yourself each time you remove or insert a view, but your question implies that you don't know how to do that. A UIStackView does know how.