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?
selectedImage.clipsToBounds=YES;
andselectedImage.contentMode=UIViewContentModeScaleAspectFit;
, but neither seem to have any effect. I assume that the imageView is connected correctly since the Storyboard shows a connection betweenselectedImage
and the ViewController underReferencing Outlets
and the circle next toselectedImage
inViewController.h
is shaded. – Alekxos