20

[[UIDevice currentDevice] orientation]方法返回纵向和横向之外的许多方向。我很清楚检查返回的方向是否“有效”,不幸的是,当我的应用程序需要特定方向来确定要运行哪些方法时,如果返回的方向不是“有效”,我无法知道哪种方法是合适的。

我已经尝试了许多解决方案,但现在我唯一能够解决的问题是检查方向是否LandscapeLeft正确LandscapeRight,如果不是,我假设屏幕是纵向的。不幸的是,当设备面朝上且屏幕处于横向时,情况并非如此。

我尝试使用父视图控制器的方向:

parentController.interfaceOrientation;

不幸的是它回来了UIDeviceOrientationUnknown。我在 SO 和 Google 上搜索遇到此问题的其他人。对我来说,苹果除了他们的定位 之外什么都没有LandscapeLeft/Right,这似乎很愚蠢。PortraitUp/Down

我真正需要的是 APPS 方向而不是设备,因为这些有时是不一致的。有什么建议吗?

4

1 回答 1

49

您是否尝试过使用UIInterfaceOrientationPortrait/ UIInterfaceOrientationLandscape?使用这些方法我通常会得到很好的结果:

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

您也可以尝试UIDeviceOrientationIsValidInterfaceOrientation

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

如果这些不起作用,您也可以尝试:

[[UIApplication sharedApplication] statusBarOrientation]
于 2011-05-16T19:24:19.123 回答