4

I'm trying without success to create a UITableViewCell (in code, no interface builder) where the cells height expands to fit it's contents. The contents being, a UILabel which itself expands given the text. And an UIImageView that should also expand to to enable it till be filled with an image (maintaining aspect ratio). So a panoramic image might result in a short height, while a portrait image, will make the image much taller. The image is downloaded, so we might not know the dimensions at first.

Even if I set a constraint to fix the image view to a 4:3 aspect, resulting in a fixed image, the constraint on the cell seems to see the height as zero instead, thus the label sits on top of the image. The image overlaps the cells above and below it. Just to get it to work, in my example I'm just using a static image resource rather than downloading an image.

I'm using a VFL on the cells containerView:

V:|-[image]-[label]-|

What could be causing the image height to be treated as zero/ignored?

enter image description here

simbolo
  • 7,279
  • 6
  • 56
  • 96
  • Try using auto layout with `tableView.rowHeight = UITableViewAutomaticDimension` – Ian Oct 23 '14 at 15:05
  • and check out http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Ian Oct 23 '14 at 15:09
  • 1
    Tried the above solutions already, but then the UIImageView isn't re-sized respecting aspect and we end up with a distorted stretched or cropped image. Adding an aspect-ratio constraint just throws up ambiguity. – simbolo Oct 23 '14 at 17:53
  • An aspect ratio constraint is not enough, you need a width or a height constraint, too. I would suggest to set the left and right margin constraints to a few pixels so the widht can be determined automatically. After that the aspect constraint makes sense and can be computed. – zisoft Oct 23 '14 at 17:59
  • You need 4 constraints for auto layout to accept it I think. If you put the image view inside a view you could set that views constraints to top, bottom, left of the screen and right of the screen. Then just resize the height of the view to always be the height of the image. That way it wouldn't matter the width of the image – Ian Oct 23 '14 at 18:05

0 Answers0