Not able to reduce UITableCustomCell height in iOS using Swift?

80 Views Asked by At

I need to reduce the height of the UITableView custom cell, if their is no data for the optional labels to show. following image would be a great help to understand

enter image description here

I tried assigning constraints constant value to 0 and 20 for the pre/post Instruction's respective top/bottom on the availability of data, it's working perfect when both optional values (pre/post instructions) are available, but in case of missing anyvalue the cell is picking the minimum height see below

enter image description here

Altough in heightForRowAt method i'm returning

return UITableViewAutomaticDimension

If any of the optional value (On the yellow label) is missing, I need to manage the exterior space of that label in the cell, Please suggest me a better solution

1

There are 1 best solutions below

2
On

you can achieve this using a UIStackView.

Just embbed the Pre Instructions, Ingredient Name and Post Instruction inside a Vertical UIStackView, then add the constrains, 20 from top and bottom, whatever you want trailing and the spacing you want from the left label. And finally add the item spacing you want between the UIStackView items (4 o 8 pts for example).

Then, what you need to do is just hide the label when it has no content and the UIStackView will do the magic for you, it will handle the spacing when an item is hidden.

;)