开发者

UIImageView not centered after rotation

开发者 https://www.devze.com 2023-01-30 00:09 出处:网络
I manually add an UIImageView to my UIView and center it, this works fine, but after rotating the device, the UIImageView isn\'t centered anymore (it\'s moved to the lower left). How can I fix this?开

I manually add an UIImageView to my UIView and center it, this works fine, but after rotating the device, the UIImageView isn't centered anymore (it's moved to the lower left). How can I fix this?开发者_运维问答

logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wi-fi-sticker.png"]];
logoView.center = self.view.center;
[self.view addSubview:logoView];

Regards, Sascha


I was able to center mine correctly every time using something like this

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration:{
       CGRect screen = [[UIScreen mainScreen] bounds];
       float pos_y, pos_x;
       pos_y = UIDeviceOrientationIsLandscape(toInterfaceOrientation) ? screen.size.width/2  : screen.size.height/2;
       pos_x = UIDeviceOrientationIsLandscape(toInterfaceOrientation) ? screen.size.height/2 : screen.size.width/2;

       myImageView.center = CGPointMake(pos_x, pos_y);
}
0

精彩评论

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