Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Layout keeps stretching UIImageView

I am using Auto layout and it's driving me crazy. I have done everything I could to prevent UIImageView from stretching all over. I have no idea why it does that. I tried using constraints, solving auto layout issues. For now, the only solution was turning the auto layout itself.

Could anyone please tell me why xcode is completely ignoring my constraints? I would expect the UIImage to stay as 320x320 when I explicitly set it as that, but noooooooo....

I want to post images, but the site won't let me, and I would like to add code to this question so that it is more specific, but there's literally no code at all. I just dragged "UIImageView" from storyboard, made it small, set constraints as is, and it's just ignoring my code.

like image 262
Gyuhyeon Lee Avatar asked Dec 30 '13 17:12

Gyuhyeon Lee


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

Does UIImageView have intrinsic content size?

A vanilla UIView instance, for example, does not have an intrinsic content size. This means that you need to add four explicit constraints to describe the size and position of a UIView instance in a user interface.

What is use of UIImageView?

Whenever you want an image in a view controller, you use a UIImageView. A UIImageView contains a UIImage, which is the raw bitmap, and allows you to configure how you want to scale or crop the image.


1 Answers

You'll want to make sure your UIImageView is set to clip to bounds. If not, the image will spill out of the view and will appear to not stretch correctly (even though in reality it is).

In interface builder, make sure the Clip Subviews box is checked.

enter image description here

If you're not using interface builder, the following code will do the same thing:

yourImageView.clipsToBounds = YES; 
like image 143
Travis Avatar answered Oct 04 '22 04:10

Travis