标签:
UIDeviceOrientation
refers to the physical orientation of the device whereas UIInterfaceOrientation
refers to the orientation of the user interface.
UIDeviceOrientation
is a property of the UIDevice
class, and there are these possible values:
UIDeviceOrientationUnknown - Can‘t be determined UIDeviceOrientationPortrait - Home button facing down UIDeviceOrientationPortraitUpsideDown - Home button facing up UIDeviceOrientationLandscapeLeft - Home button facing right UIDeviceOrientationLandscapeRight - Home button facing left UIDeviceOrientationFaceUp - Device is flat, with screen facing up UIDeviceOrientationFaceDown - Device is flat, with screen facing down
As for UIInterfaceOrientation
, it is a property of UIApplication
and only contains 4 possibilities which correspond to the orientation of the status bar:
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait, UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight, UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
获取设备方向UIDeviceOrientation
:
[[UIDevice currentDevice] orientation]
获取用户界面方向 UIInterfaceOrientation
,
[[UIApplication sharedApplication] statusBarOrientation]
// 设备方向改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOrientChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; ? - (void)orientationChanged:(NSNotification *)notification{ [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; } ? - (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation { ? switch (orientation) { case UIInterfaceOrientationPortrait: case UIInterfaceOrientationPortraitUpsideDown: { //load the portrait view } ? break; case UIInterfaceOrientationLandscapeLeft: case UIInterfaceOrientationLandscapeRight: { //load the landscape view } break; case UIInterfaceOrientationUnknown:break; } } ?
// 用户界面方向改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; ? - (void)didChangeOrientation:(NSNotification *)notification { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; ? if (UIInterfaceOrientationIsLandscape(orientation)) { NSLog(@"Landscape"); } else { NSLog(@"Portrait") } }
用NSNotificationCenter记得在dealloc方法中remove观察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
判断横竖屏
BOOL isLandscape = self.view.frame.size.width > self.view.frame.size.height;
当controllers不是全屏时,这种方法有问题。
作者:sue_zheng
出处:http://www.cnblogs.com/sueZheng/p/4954938.html
标签:
原文地址:http://www.cnblogs.com/sueZheng/p/4960395.html