Tarvo Mäesepp Tarvo Mäesepp - 1 month ago 10
Swift Question

Self sizing cell makes views to stretch

I tried to make self-sizing cells. I got it to work but with some bug. Which I think is because of wrong constraits. So I kindly ask you if you could look them.

Problem is that after scrolling in tableview, all my views are going to stretch. I thought that it would happen because of not setting height constraint but it seems not to be problem.

Illustration(pretty normal):

enter image description here

After scrolling and coming back up:

enter image description here

And these are my constraints for this view which all of them looks good to me:

enter image description here

And if you want to know how I made cells to resize, then like this:

tableView.estimatedRowHeight = 105//Because my row height is 105
tableView.rowHeight = UITableViewAutomaticDimension

Answer

When this sort of thing happens, the implication is that you are not supplying sufficient constraints to size the cell's contentView from the inside out. You can readily confirm this using View Debugging to pause the app when you see your incorrectly sized cell; you will then be able to study the actual values of your constraints and see what the problem is.

EDIT Now that you've posted your constraints, it's easy to see what the problem is: There is nothing at all connecting your interface elements to the bottom of the contentView. But that is the whole point of self-sizing cells: you must have, as I already said, sufficient constraints running all the way from the top of the contentView to the bottom of the contentView. That is what determines the height — and so you are not determining it.