20

I'm trying to find out if the device is in portrait or landscape mode. My code works quite well if the device is not facing up. If it does face up (and orientation == 5), it won't distinguish between portrait and landscape. Is there anyway to determine the "orientation" in terms of landscape / portrait if the UIDeviceOrientation is FaceUp?

My code:

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

NSLog(@"orientation: %d", interfaceOrientation);

if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
    NSLog(@"LANDSCAPE!!!");
}

if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
    NSLog(@"PORTRAIT!!!");
}
4

3 回答 3

42

你不应该混淆UIDeviceOrientationand UIInterfaceOrientation,它们不同但相关,如它们的声明所示

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

UIDeviceOrientation告诉你设备的方向是什么。UIInterfaceOrientation告诉您界面的方向是什么,并由UIViewController. UIInterfaceOrientation显然是纵向或横向,而UIDeviceOrientation可以有不明确的值(UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown, UIDeviceOrientationUnknown)。

在任何情况下,您都不应该尝试使用 来确定 UIViewController 的方向[[UIDevice currentDevice] orientation],因为无论设备方向是什么,UIViewController interfaceOrientation属性都可能不同(例如,如果您的应用程序根本不旋转到横向,而[[UIDevice currentDevice] orientation]可以是)。UIDeviceOrientationLandscapeLeftviewController.interfaceOrientationUIInterfaceOrientationPortrait

更新: 从 iOS 8.0 开始,[UIViewController interfaceOrientation]已弃用。这里提供的另一种选择是[[UIApplication sharedApplication] statusBarOrientation]. 这也返回UIInterfaceOrientation

于 2011-11-05T09:18:18.043 回答
5

我制作这个代码框架来处理想要的和不需要的设备方向,在我的情况下,我想忽略 UIDeviceOrientationUnknown、UIDeviceOrientationFaceUp 和 UIDeviceOrientationFaceDown,缓存最后允许的方向。此代码处理 iPhone 和 iPad 设备,可能对您有用。

- (void)modXibFromRotation {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSString *device = [[UIDevice currentDevice]localizedModel];
    UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];

    if ([device isEqualToString:@"iPad"]) {

        if (orientation == UIDeviceOrientationUnknown || 
            orientation == UIDeviceOrientationFaceUp || 
            orientation == UIDeviceOrientationFaceDown) {

                orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

            /* Your code */
        }

        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

            /* Your code */     
        }
    }

    if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {

        if (orientation == UIDeviceOrientationUnknown || 
        orientation == UIDeviceOrientationFaceUp || 
        orientation == UIDeviceOrientationFaceDown) {

            orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

             /* Your code */
        }

        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

            /* Your code */
        }
    }
}
于 2012-01-04T03:38:26.927 回答
1

从 iOS13 开始使用

UIInterfaceOrientation orientation;
if (@available(iOS 13.0, *)) {
    orientation = self.window.windowScene.interfaceOrientation;
} else {
    orientation = [[UIApplication sharedApplication] statusBarOrientation];
}
于 2019-12-10T13:43:29.360 回答