Home Ask Login Register

Developers Planet

Your answer is one click away!

Sweeper February 2016

How to configure the image or image view so that the image keeps its aspect ratio no matter what the size of the image view is?

I put an image view in a static table view cell. The image is now being stretched to fit the whole view. I want the image to:

  • be centered in the image view
  • be in the largest size possible but
  • not exceed the bounds of the image view and
  • keeps the aspect ratio of the image

First I went to the docs of UIImageView. But didn't find anything useful. Then I looked at the docs of UIImage and found UIImageResizingMode. But that enum only contains Stretch and Tile, which is not what I wanted. I then found this post which told me to

logo.contentMode = .Center

But the image is still stretched. I then tried this

logo.contentMode = .ScaleAspectFill

But the result is the same.

Please tell me a good way to do this!

Additional info:

The image view has the following constraints:

enter image description here

Answers


Matt39 February 2016

You have to set the contentMode to UIViewContentModeScaleAspectFit.

Post Status

Asked in February 2016
Viewed 3,800 times
Voted 9
Answered 1 times

Search




Leave an answer


Quote of the day: live life