R. Dewi R. Dewi - 1 year ago 89
iOS Question

How to stop an image from stretching within a UIImageView?

I have a

where I have set the frame size to
x = 0, y = 0, width = 404, height = 712
In my project, I need to change the image in

I am using this code to change the image:

self.imageView.image = [UIImage imageNamed:@"setting-image.png"];

The problem is, when the
image size is smaller than the
frame size, the image stretches. I don't want it to stretch. Is there any way to do that?

Answer Source

You can use

self.imageView.contentMode = UIViewContentModeScaleAspectFit;


imageView.contentMode = .ScaleAspectFit

Or UIViewContentModeCenter, or any of the other modes described in the UIView documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download