UIImageview in custom cell with Aspect fill mode constraints issue

1k views Asked by At

I have an issue with UIImageView in UITabelViewCell.

here is my UIImageView in the interface builder and it's constraints

enter image description here

When i set the mode "scale to fill" i got this result (which match the constraints and my expectations)

enter image description here but I don't want that mode!!

What I want is "Aspect Fill", but sitting this mode give me this result

enter image description here

How Can I handle this issue ?

3

There are 3 answers

0
jrturton On

Using aspect fill keeps the proportions but may result in the image being drawn outside the bounds of the view. You need to clip the subviews on the image view (you can see this option in the interface builder screenshots above) and then the excess parts of the image will not be drawn.

If you want to keep proportions and also see the entire image, then you need Aspect Fit, but this will result in blank areas to the sides of the image if it doesn't match the aspect ratio of the view.

1
DilumN On

There is a constrains mismatch for your UIImageView,

Use below constrains only,

Top Space -> ContentView

Bottom Space -> ContentView

Trailing Space -> ContentView

Leading Space -> Only One Label

or

Top Space -> ContentView

Height -> 161

Leading Space -> Only One Label

Width -> 151
2
Sanjay Mohnani On

While selecting the content mode to Aspect Fill for the image view, the content is getting out of the bounds of the image view. This is because in aspect fill mode the content is filled till the time the bounds of the container (image view) are fully filled with the content by keeping the aspect ratio intact. And when the content is fully filled it get's out of the bounds either width wise or height wise.

In order to keep the content mode to Aspect Fill for UIImageView you should also set it's Clip Subview's property as well. Setting this property will clip the content of the image view outside the bounds.