Reputation: 512
How should I programmatically resize the dimensions of a UIImageView created in a Storyboard file to match those of a specific image? Setting either the frame or bounds of the UIImageView to that of the selected image does not change the size of the imageView in the Xcode iOS simulator. I have looked at many other similar problems and tried their answers, but none of the fixes that they have given seem to have worked so far.
Here is the code used to attempt to resize the UIImageView:
-(void)viewWillAppear:(BOOL)animated{
//Get selected image
UIImage *selectedImage=[UIImage imageNamed:selectedItem.pictureName];
//Set imageView's image to selected image
imageView.image=selectedImage;
//Make new frame with dimensions of selected image and same origin
CGRect newImageViewFrame=CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,selectedImage.size.width,selectedImage.size.height);
//Set imageView's frame to match that of the newly created frame
imageView.frame=newImageViewFrame;
[super viewWillAppear:animated];
}
The only way that the imageView has resized correctly is by replacing a static imageView with an imageView created programmatically to match the frame of the selectedImage
. It seems strange that this should work when creating a new UIImageView from scratch, but not when trying to resize an existing UIImageView.
My project is currently using AutoLayout, but I have not added any constraints to the relative UIImageView. Is this maybe where the problem is coming from?
Upvotes: 0
Views: 1608
Reputation: 9915
My project is currently using AutoLayout, but I have not added any constraints to the relative UIImageView. Is this maybe where the problem is coming from?
Yes. When you don't specify constraint, you automatically get "fixed frame" constraints. The constraints for you image view would look something like this:
<NSIBPrototypingLayoutConstraint:0x7f95a943bad0 'IB auto generated at build time for view with fixed frame' H:|-(87)-[UIImageView:0x7f95a9b112b0](LTR) (Names: '|':UIView:0x7f95a974e7c0 )>,
<NSIBPrototypingLayoutConstraint:0x7f95a943bfe0 'IB auto generated at build time for view with fixed frame' V:|-(217.5)-[UIImageView:0x7f95a9b112b0] (Names: '|':UIView:0x7f95a974e7c0 )>,
<NSIBPrototypingLayoutConstraint:0x7f95a943c030 'IB auto generated at build time for view with fixed frame' H:[UIImageView:0x7f95a9b112b0(146)]>,
<NSIBPrototypingLayoutConstraint:0x7f95a943c080 'IB auto generated at build time for view with fixed frame' V:[UIImageView:0x7f95a9b112b0(132)]>,
So you attempts to modify the frame are overwritten when the fixed frame constraints are re-applied.
What you need to do is establish horizontal and vertical position constraints. Once you do this, you will no longer get the fixed frame constraints. And since you haven't constrained the size, it will be determined by the intrinsicContentSize
property, which will be the size of the image.
One more detail is, if you're not setting an image in the storyboard, you'll need to specify a placeholder intrinsic content size in the Size Inspector or you'll get missing constraint errors in IB.
Upvotes: 3