开发者

UIImageView Background Image and Rotation Help

开发者 https://www.devze.com 2023-03-02 19:07 出处:网络
In my iPad app I currently have a background image png of 1024x1024 pixels and set it using the following

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 luc开发者_StackOverflow社区k, 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];


Check the autoresizingMask property for the imageView.

0

精彩评论

暂无评论...
验证码 换一张
取 消