Search code examples
iosswiftuiimageviewuitableview

UIImage in self sizing UITableViewCell stretching


So I have a tableview setup so it displays some information text with an image, the text can vary so I've set up the constraints that the cell updates its size by itself, however the images are stretching as shown below and i've checked that the content mode is set to Aspect Fit. Image stretched

As it can be observed from the image there are 3 Views in the cell: -The Title -The Image, which is being stretched -The description

My constraints are set up as followed:

  1. Title.top = topMargin
  2. Title.trailing = trailingMargin
  3. Title.leading = leadingMargin
  4. Title.height = 32

  5. Image.leading = leadingMargin

  6. Image.top = Title.bottom + 16
  7. bottom.margin = Image.bottom
  8. Image.width = SuperView(content of cell).width * 0.4

  9. text.leading = image.trailing + 16

  10. trailingMargin = text.trailing
  11. bottomMargin = text.bottom
  12. text.top = topMargin + 16

How can I fix this issue so the images don't stretch? If any additional information is needed please ask and i'll add it.

Thanks -Jorge


Solution

  • Add a height constraint to the image matching its width so that it maintains its square aspect ratio. Or any height for that matter. Just fix the height so that it does not stretch.

    image.height = SuperView(content of cell).width * 0.4