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
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];