Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a max height for UIImageView regardless of final Image size

I have an issue where I have a UIImageView with mode as "Aspect Fill". I have set dimensions for my UIImageView, but I've noticed at runtime, the height of the UIImageView tends to change based on the final image rendered after it's been filled.

Is there a way to set a max height for the UIImageView regardless of the height of the content that fills it. Basically what I want is something similar to the kind of functionality we have in CSS whereby I can perform an "overflow: hidden" type of command, so that anything beyond the size of the UIImageView is hidden/ignored.

Is there a way to accomplish this in iOS?

like image 894
Lavvo Avatar asked Dec 02 '22 15:12

Lavvo


2 Answers

Yes, there are two possible causes here:

a) The UIImageView might be growing because of the auto-layout constraints applied to it: If you are using auto-layout just set a constraint to the UIImageView with a maximum (or fixed) height.

enter image description here

b) The UIImageView might not be growing at all but you are seeing the "excess" of the image because the view is not clipping it. Just set the clipsToBounds to true either on Interface Builder or from your code.

like image 98
fz. Avatar answered Dec 15 '22 16:12

fz.


Use "Scale to Fill", it will scale the image according to the UIImageView size.

"Aspect Fill" will not shrink the UIImageView, but will only print on a part of it.

like image 35
TheSquad Avatar answered Dec 15 '22 15:12

TheSquad