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

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

我已经尝试了一些解决scheme,但现在唯一能够解决的问题是检查方向是LandscapeLeft还是LandscapeRight ,如果不是,我认为屏幕是纵向的。 不幸的是,当设备面朝上并且屏幕处于横向时,这不是真的。

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

 parentController.interfaceOrientation; 

不幸的是,它返回UIDeviceOrientationUnknown 。 我search和谷歌search谁遇到了这个问题的其他人。 对我来说,苹果似乎很愚蠢,除了LandscapeLeft/RightPortraitUp/Down ,其他的方向都没有。

我真正需要的是应用程序的方向而不是设备,因为这些有时是不一致的。 有什么build议?

你有没有尝试过使用UIInterfaceOrientationPortrait / UIInterfaceOrientationLandscape ? 我通常使用这些方法有很好的结果:

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

您也可以尝试UIDeviceOrientationIsValidInterfaceOrientation 。

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

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

 [[UIApplication sharedApplication] statusBarOrientation]