Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to auto-size a UIImageView in Interface Builder/storyboard?

When you set a image for a uiimageview in storyboard or interface builder, it retains the shape of the original UIImageView, which means that the image itself is completely distorted.

To match the size of the UIImageView to its image, I have to manually enter in the size from the file inspector.

Is there a way that UIImageView can automatically detect the native size of its image and size its self accordingly?

like image 242
zakdances Avatar asked Aug 15 '12 09:08

zakdances


2 Answers

I am assuming you are referring to layout in storyboard/IB. To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.

like image 135
Nelson Brian Avatar answered Sep 21 '22 11:09

Nelson Brian


Why don't you set the size of your uiimageview in code? Here's what you need to do...

//get the Image
UIImage *img = [UIImage imageNamed:@"img.png"];

CGFloat x = img.origin.x;
CGFloat y = img.origin.y;
CGFloat width = img.size.width;
CGFloat height = img.size.height;
imageView.frame = CGRectMake(x, y, width, height);  //imageView is your uiimageview's reference
like image 43
Humayun Avatar answered Sep 22 '22 11:09

Humayun