I have a UIImageView
inside a UITableViewCell
. The cell height changes based on the number of lines of text in a UILabel
using estimated row heights. I want this image to be centered vertically in the cell, with a fixed width, and height >= 40 and <=80. As the cell height increases I want the image to increase to that max height then stop, always remaining vertically centered. To do this, I added those two height constraints and the width constraint, plus constraints to the top and bottom of the cell with a constant of 5 for some padding and a priority of 250. Adding a background color to the image view you can see it is being laid out as expected. But when I set the image on the image view, the height is changing unexpectedly.
With 3 lines in the label:
With 6 lines in the label:
Why is that? How can I make sure the image in the image view doesn't affect the image view size, as I want the cell height determined by the auto layout constraints to determine it?
While I wasn't able to get the expected image view height using those auto layout constraints, I did achieve the goal by using a single height constraint and manually setting its constant
in layoutSubviews
of the UITableViewCell
, calculated to be max(40, min(contentView.bounds.height - 5 * 2, 80))
. And then added a CenterY constraint to the superview to keep it vertically centered. Now adding an image to the image view doesn't unexpectedly change the image view height.