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

Jos*_*hua 20 screen-orientation uiinterfaceorientation ios

[[UIDevice currentDevice] orientation]方法返回超出纵向和横向方向的多个方向.我很清楚检查返回的方向是否"有效",不幸的是,当我的应用程序需要特定的方向来确定运行哪些方法时返回的方向不是"有效",我无法知道哪种方法是适当.

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

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

parentController.interfaceOrientation;
Run Code Online (Sandbox Code Playgroud)

不幸的是它返回UIDeviceOrientationUnknown.我搜索SO和谷歌搜索遇到此问题的其他人.看来愚蠢,我认为苹果将有什么,但LandscapeLeft/RightPortraitUp/Down他们的方位.

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

小智 49

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

if(UIInterfaceOrientationIsPortrait(viewController.interfaceOrientation)) {
    //do portrait work
} else if(UIInterfaceOrientationIsLandscape(viewController.interfaceOrientation)){
    //do landscape work
}
Run Code Online (Sandbox Code Playgroud)

您也可以尝试UIDeviceOrientationIsValidInterfaceOrientation.

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

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

[[UIApplication sharedApplication] statusBarOrientation]
Run Code Online (Sandbox Code Playgroud)