Lavvo Lavvo - 7 months ago 27
iOS Question

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?

fz. fz.
Answer Source

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.

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