简体   繁体   中英

IOS center bottom position view

I do the following to a loading animation, to place it at the bottom center of the screen.

CGPoint bottomCenter = CGPointMake((self.imageView.bounds.size.width / 2), (self.imageView.bounds.size.height * 0.8));  
    self.activityView.center = bottomCenter;

(imageView is the full screen splash image)

If the orientation is portrait, it is positioned perfectly, however turning on its side, in landscape or upside down portrait and the animation ends up miles away :S

Does anyone know the correct way to position this loading animation, its for the splash screen.

使用UIDeviceOrientationIsLandscape(deviceOrientation)UIDeviceOrientationIsPortrait(deviceOrientation)代替deviceOrientation==X

First, look at tkanzakic's answer. Second, don't use screen bounds, orientate the view by its parent view. If you put your view to the bottom of your parent view and you set the autoresizing mask correctly, everything will be done automatically - no need to check device orientation.

Try this code

CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGPoint bottomCenter;
if (deviceOrientation == 1) {

bottomCenter = CGPointMake((screenBounds.size.width / 2), (screenBounds.size.height * 0.8));
} 
else if (deviceOrientation == 4) {
bottomCenter = CGPointMake((screenBounds.size.width / 2), (screenBounds.size.height * 0.8));
}

In landscape mode you are taking wrong width and height

In landscape mode width and height also change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM