I\'ve spent several days trying to figure this out, but there doesn\'t seem to be a solution. I have a very basic UITableView cell with two labels in it. One of them will be
First of all, unless you need a UIStackView
for some other purpose that you're not mentioning in your question, don't use them. Multi-line labels and dynamic height cells work well without them (don't needlessly complicate the code if they are not necessary).
There are no bugs in iOS that stop this from working, that I'm aware of. Dynamically sized cells work great with Auto Layout, all you need to do is set the correct constraints in the cells and set the rowHeight
and estimatedRowHeight
properties of the table view.
The most likely reason this is not working for you is that you have not put all the needed constraints in your cell. The needed constraints are:
I think you're missing point 4.
Edit
According to your comments, you're using a UIStackView
so as to align the top baselines between the single and the multi-line labels. By this, I understand you want your labels to align like this:
To get this behaviour, you don't need a UIStackView
. Just give the labels constraints to allow the multi-line label to define the cell height. The following image shows the top baselines aligned between the two labels (i.e. the first lines of each label are aligned).
Notice how the left label only needs a leading and top constraint, while the right label needs a top, trailing and bottom, because it defines cell height.
In summary, dynamic table view cells require three things:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100