确定屏幕的方向而不是设备的方向

20
[[UIDevice currentDevice] orientation]方法返回的方向数不仅限于肖像和风景方向。我很清楚需要检查返回的方向是否“有效”,但不幸的是,如果返回的方向不“有效”,而我的应用程序需要特定方向来确定要运行哪些方法,那么我就无法知道哪种方法是适当的。
我尝试了许多解决方案,但目前唯一能找到的方法是检查方向是否为LandscapeLeftLandscapeRight,如果不是,则假设屏幕处于纵向模式。不幸的是,当设备面朝上且屏幕处于横向模式时,这种情况并不正确。
我尝试使用父视图控制器的方向:
parentController.interfaceOrientation;

不幸的是,它返回了UIDeviceOrientationUnknown。我在SO和Google上搜索了一下,看看有没有其他人遇到过这个问题。对我来说,苹果竟然除了LandscapeLeft/RightPortraitUp/Down之外还有其他朝向方法,这似乎很傻。

我真正需要的是应用程序的方向,而不是设备的方向,因为有时两者可能不一致。有什么建议吗?

1个回答

49

你尝试使用过 UIInterfaceOrientationPortrait/UIInterfaceOrientationLandscape 吗?我通常使用这些方法效果非常好:

if(UIInterfaceOrientationIsPortrait(viewController.interfaceOrientation)) {
    //do portrait work
} else if(UIInterfaceOrientationIsLandscape(viewController.interfaceOrientation)){
    //do landscape work
}

你也可以尝试使用 UIDeviceOrientationIsValidInterfaceOrientation

它们只会返回应用程序的方向,可能与设备的方向不同。

如果上述方法不起作用,您还可以尝试:

[[UIApplication sharedApplication] statusBarOrientation]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接