Kenny Wyland
Kenny Wyland

Reputation: 21870

UIImageView contentMode scale aspect fill is stretching the image

I set my UIImageView.contentMode = UIViewContentModeScaleAspectFill, but it is still stretching the image. (Not a clipping issue, it is stretching the image.)

It even shows it stretched in my Storyboard. I created an image of green and brown squares to test:

enter image description here

I added a UIImageView in my Storyboard. I added the image to that image view, and when I'm setting to Aspect FIT then it does what it is supposed to do (maintain aspect ratio and show the whole image allowing gaps as needed), but when I switch to Aspect FILL then it stretches the image. Here is an animated gif from my storyboard:

enter image description here

What is going on? This is in a storyboard in a project that is a few years old. If I create a brand new project with a brand new storyboard, then this insanity doesn't happen.

Is there something in my underlying storyboard that is causing this bug?

Upvotes: 2

Views: 1569

Answers (1)

TylerP
TylerP

Reputation: 9829

It's not stretching it. It's filling the area while keeping the aspect ratio. If it were stretching it, the horizontal white line in the middle would not be as tall as the vertical white lines are wide.

Set the content mode to "Scale To Fill" to see what it would look like if it were getting stretched (like this):

Scale To Fill

Note the difference in line thickness of the middle white line, as opposed to when it's set to Aspect Fill:

Aspect Fill With Clip to Bounds Enabled

If you turn off Clip to Bounds on the image view, then you can see how the image is getting cropped when set to Aspect Fill:

Aspect Fill With Clip to Bounds Disabled

If you want to show the full image at the full width while keeping the aspect ratio, you'll have to increase that cell's height to accommodate the height of the image.

Upvotes: 1

Related Questions