Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView Background Image and Rotation Help

In my iPad app I currently have a background image png of 1024x1024 pixels and set it using the following

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];

This adds the UIImageView fine and the view is centered in landscape however it is off center in portrait.

I have played around with some of the contentMode settings but not had any luck, I either get portrait to be center or landscape to be center but not both.

Could someone please help me with getting the UIImageView centered in both portrait and landscape after rotation.

Thanks in advance Matt

like image 695
Matt Price Avatar asked May 02 '11 11:05

Matt Price


Video Answer


2 Answers

You need to set the contentMode, the frame and an appropriate autoresizing mask:

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background-image"]];
imgView.frame = self.view.bounds;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imgView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];
like image 110
omz Avatar answered Oct 06 '22 21:10

omz


Check the autoresizingMask property for the imageView.

like image 31
visakh7 Avatar answered Oct 06 '22 22:10

visakh7