如何以编程方式确定iPhone界面方向?
如何以编程方式确定iPhone界面方向?,iphone,cocoa-touch,Iphone,Cocoa Touch,UIDevice.orientation的可能值包括UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown。虽然知道设备是平面的可能很有用,但这并不能告诉我们它是平面的,以纵向还是横向模式显示界面。在设备返回不明确信息的情况下,是否有办法找到GUI的当前方向?我想我可以跟踪方向更改事件,以记住最后一个纵向/横向方向,或者检查主UIView的边界高度和宽度,但这似乎很困难。设备或UIView上是否有我缺少的属性?在viewcontroller中,
UIDevice.orientation
的可能值包括UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
。虽然知道设备是平面的可能很有用,但这并不能告诉我们它是平面的,以纵向还是横向模式显示界面。在设备返回不明确信息的情况下,是否有办法找到GUI的当前方向?我想我可以跟踪方向更改事件,以记住最后一个纵向/横向方向,或者检查主UIView的边界高度和宽度,但这似乎很困难。设备或UIView上是否有我缺少的属性?在viewcontroller中,您只需使用以下代码:
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
UIInterfaceOrientation是一个枚举:
typedef enum {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeRight
} UIInterfaceOrientation;
我也见过同样的情况。具体而言,在该流程中:
- 控制器1加载,例如。。肖像画
- 将另一个控制器推到堆栈上
- 在第二个控制器中,将设备方向旋转到横向
- 将设备平放在桌子上,然后弹回到原始控制器
在我的例子中,视图的边界宽度检查用于确定事物的真实状态,但我想知道其他人是否有不同的处理方式。您可以通过3种方式获得方向:
UIInterfaceOrientation-orientation=self.interfaceOrientation代码>
返回UIInterfaceOrientation,即
接口。它是UIViewController中的属性,您可以访问
这个仅在UIViewController类中使用
UIInterfaceOrientation方向=[[UIApplication sharedApplication]statusBarOrientation]代码>
返回UIInterfaceOrientation,即
应用程序的状态栏。您可以在中的任何位置访问该属性
应用程序的点。我的经验表明,这是最重要的
检索真实界面方向的有效方法
UIDeviceOrientation方向=[[UIDevice currentDevice]
方向]代码>返回UIDeviceOrientation,设备方向。
您可以在的任何位置访问该属性
应用程序。但请注意,UIDeviceOrientation并不总是如此
界面定向。例如,当您的设备位于平原上时
表中,您可能会收到意外值
状态栏方向(statusBarOrientation)始终返回界面方向,即使状态栏已隐藏 可以在没有视图控制器的情况下使用状态栏方向。它为您提供当前视图方向,而不是设备方向
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
如果您只关心设备是横向还是纵向,那么viewcontroller上有一些非常方便的方法:
UIDeviceOrientationIsLandscape(self.interfaceOrientation)
UIDeviceOrientationIsPortrait(self.interfaceOrientation)
以下是您可能会发现有用的代码片段:
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
NSLog( @" ORIENTATION: %@", UIInterfaceOrientationIsLandscape( orientation ) ? @"LANDSCAPE" : @"PORTRAIT");
这不是UIInterfaceOrientation,而是-UIDeviceOrientation,这是micco在他的帖子中提到的。你是对的。UIDeviceOrientation并不总是UIInterfaceOrientation。这困扰了我很多年。谢谢。[[UIDevice currentDevice]orientation]如果用户已从跳板锁定其设备方向,则不起作用。方向始终是最后一个值,并且没有更改通知。在阅读此答案之前,我已经使用了大量的变通方法,因为我的类并不都是UIView…这非常有效!如果可以的话,我会投一百万票的…非常感谢@我需要开发面向所有界面的iphone应用程序,而不是我应该做什么。。?是否有任何文档文件或博客教程,请告诉我..“不要使用此属性(UIViewController.interfaceOrientation)来通知布局决策。相反,请使用UIApplication类参考中描述的statusBarOrientation属性。”说AppledPrecated自从iOS 8I以来就一直使用状态栏方向作为iPad上的工作参考点。邦德的检查对我不起作用。