【问题标题】:How to programmatically determine iPhone interface orientation?如何以编程方式确定 iPhone 界面方向?
【发布时间】:2010-10-12 16:40:07
【问题描述】:

UIDevice.orientation 的可能值包括UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown。虽然知道设备是平面的可能很有用,但这并不能告诉我们它是否是平面的,显示面向纵向或横向模式的界面。在设备返回模糊信息的情况下,有没有办法找到 GUI 的当前方向?我想我可以跟踪方向更改事件以记住最后的纵向/横向方向或检查主 UIView 的边界高度和宽度,但这似乎很麻烦。设备或 UIView 上是否有我缺少的属性?

【问题讨论】:

    标签: iphone cocoa-touch


    【解决方案1】:

    您可以通过 3 种方式获得方向:

    1. UIInterfaceOrientation orientation = self.interfaceOrientation; 返回 UIInterfaceOrientation,当前方向 界面。它是 UIViewController 中的一个属性,你可以访问 这个仅在 UIViewController 类中

    2. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 返回 UIInterfaceOrientation,当前方向 应用程序的状态栏。您可以在 any 中访问该属性 您的应用程序的重点。我的经验表明这是最 检索真实界面方向的有效方法

    3. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 返回UIDeviceOrientation,设备方向。 您可以在您的任何时间点访问该属性 应用程序。但请注意 UIDeviceOrientation 并不总是 UI界面方向。例如,当您的设备在平原上时 表,你可以收到意想不到的价值。

    【讨论】:

    • 这不是 UIInterfaceOrientation,它是 - UIDeviceOrientation,这就是 micco 在他的帖子中已经提到的。
    • 你是对的。 UIDeviceOrientation 并不总是 UIInterfaceOrientation。
    • 这困扰了我很久。谢谢。
    • [[UIDevice currentDevice]orientation] 如果用户已从 Springboard 锁定了他的设备方向,则不起作用。方向始终是最后一个值,并且没有更改通知。
    • 在阅读此答案之前,我一直在使用大量的变通方法,因为我的课程并非都是 UIViews ......这非常好用!如果可以的话,我会为此投一百万票……非常感谢! @铍
    【解决方案2】:

    这里有一个你可能会觉得有用的代码:

    UIInterfaceOrientation  orientation = [UIDevice currentDevice].orientation;
    NSLog( @" ORIENTATION: %@", UIInterfaceOrientationIsLandscape( orientation ) ? @"LANDSCAPE" : @"PORTRAIT");
    

    【讨论】:

      【解决方案3】:

      如果您只关心设备是横向还是纵向,则视图控制器上有一些不错的便捷方法:

      UIDeviceOrientationIsLandscape(self.interfaceOrientation)
      UIDeviceOrientationIsPortrait(self.interfaceOrientation)
      

      【讨论】:

        【解决方案4】:

        状态栏方向 (statusBarOrientation) 总是返回界面方向,即使状态栏被隐藏。

        您可以在没有视图控制器的情况下使用状态栏方向。它为您提供当前视图方向,而不是设备方向。

        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        

        【讨论】:

          【解决方案5】:

          我也见过同样的情况。特别是在这个流程中:

          • 控制器 1 加载在说.. 纵向。
          • 将另一个控制器推入堆栈
          • 在第二个控制器中,将设备方向旋转为横向。
          • 将设备平放在桌子上,然后弹回原来的控制器。

          此时,我看到的任何方向检查都会返回无效的方向 5,因此无法直接确定应该使用横向还是纵向布局。 (我正在按方向进行自定义布局定位,所以这是重要的信息)

          在我的例子中,视图的边界宽度检查用于确定事物的真实状态,但我很想知道其他人是否有不同的处理方式。

          【讨论】:

          • 我使用状态栏方向作为 iPad 上的工作参考点。 Bound 的检查对我不起作用。
          【解决方案6】:

          在视图控制器中,您可以简单地使用代码:

          UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
          

          UIInterfaceOrientation 是一个枚举:

          typedef enum {
            UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
            UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
            UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeLeft,
            UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeRight
          } UIInterfaceOrientation;
          

          【讨论】:

          • 如果我需要开发具有所有界面方向的 iphone 应用程序,我应该做什么..?是否有任何文档文件或博客教程,请告诉我..
          • “不要使用此属性 (UIViewController.interfaceOrientation) 来通知布局决策。而是使用 UIApplication 类参考中描述的 statusBarOrientation 属性。”苹果说
          • 自 iOS 8 起已弃用
          猜你喜欢
          • 1970-01-01
          • 2014-07-15
          • 1970-01-01
          • 1970-01-01
          • 2010-11-11
          • 2015-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多