In my iPhone app I'm trying to programmatically resize and move a UIImageView in order to give it as origin the top-left corner of the iPhone screen and to have it to be both wide and tall as the height of the screen. To do so I'm using with the following code:
-(void)viewDidLoad
{
//Getting screen size
CGRect screenSize = [[UIScreen mainScreen]bounds];
//Creating the new frame for the UIImageView
CGRect arect = CGRectMake(0, 0, screenSize.size.height, screenSize.size.height);
//Setting the image to show
UIImage *image = [UIImage imageNamed:@"Adele.jpg"];
self.imageView.image = image;
//Setting new frame and contentmode
self.imageView.frame = arect;
self.imageView.contentMode = UIViewContentModeScaleToFill;
}
self.imageView is an outlet I created with InterfaceBuilder.
Problem is that this code does not seem to have any effect on the frame of the UIImageView when I start the application. The imageview infact will be shown in the same position and with the same size that I set when I created it with IB.
Does anyone of you guys have an idea on why this code isn't working? What am I missing?
I think you might need a different content mode.
Give UIViewContentModeScaleAspectFit a try. In context:
self.imageView.frame = arect;
self.imageView.contentMode = UIViewContentModeScaleToFill;
If that doesn't work you can initWithFrame:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With