确定屏幕的方向而不是设备的方向 [英] Determining the Orientation of the screen rather than the orientation of the device

查看:99
本文介绍了确定屏幕的方向而不是设备的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The [[UIDevice currentDevice] orientation] method returns a number of orientations beyond the portrait and landscape orientation. I am well aware of checking to see if the orientation returned is "valid", unfortunately if the orientation returned is NOT "valid" when my app requires a specific orientation to determine which methods to run, I have no way of knowing which method is appropriate.

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

I have attempted a number of solutions to this, but for now the only thing I have been able to work out is to check to see if the orientation is LandscapeLeft or LandscapeRight and if it isn't I assume the screen is in portrait orientation. Unfortunately this isn't true when the device is face up and the screen is in landscape orientation.

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

I attempted to use the parent view controller's orientation:

parentController.interfaceOrientation;

不幸的是它返回 UIDeviceOrientationUnknown 。我搜索SO和Google搜索遇到此问题的其他人。对我来说,除了 LandscapeLeft / Right PortraitUp / Down 之外,对我来说,这对我来说似乎很愚蠢。

Unfortunately it returned UIDeviceOrientationUnknown. I search SO and Google for someone else who has faced this problem. It seems silly to me that Apple would have anything but LandscapeLeft/Right and PortraitUp/Down for their orientations.

我真正需要的是APPS方向而不是设备,因为这些方法有时会出现问题。有什么建议吗?

What I really need is the APPS orientation not the devices, since these are sometimes at odds. Any advice?

推荐答案

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

Have you tried using UIInterfaceOrientationPortrait/UIInterfaceOrientationLandscape? I usually have pretty good results using these methods:

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

您也可以尝试 UIDeviceOrientationIsValidInterfaceOrientation

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

These will only return the app's orientation, which may not be the same as the device's orientation.

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

If those don't work, you can also try:

[[UIApplication sharedApplication] statusBarOrientation]

这篇关于确定屏幕的方向而不是设备的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆