Resize image with autolayout

My app loads an image at runtime and I want to display it inside a table cell. To do that I created a UIView (trailing/leading/top/bottom: 10) inside the cell and added a UIImageView as its child.

What constraints do I need for the child, so the image keeps its proper aspect ration, isn't wider than the width of the screen and there's no unused space (apart from the constraints)?

I tried to use:

  • Trailing/leading/top/bottom: 0
  • Align center x/y

This works if the image uses the portrait format but with landscape there's unused space above and below the image. I also tried to set the view's height to ">=50" but no success and I can't just set a max. value because my app has to work on both iPhones and iPads.

Did you set the UIImageView content Mode ?

You should try Aspect Fit or Aspect Fill.

Can you show some screen shots representing your current settings and the current result?

Resize image with autolayout
 
 
Q