【问题标题】:supportedInterfaceOrientations method not working on iOS 6 UIViewControllersupportedInterfaceOrientations 方法在 iOS 6 UIViewController 上不起作用
【发布时间】:2012-09-21 20:10:08
【问题描述】:

在iOS 5中,我的应用程序使用了改变方向的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

在 iOS 6 中,我想我应该这样做,但它什么也没做!我的应用旋转不是我想要的方式。

- (BOOL) shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

【问题讨论】:

    标签: uiviewcontroller orientation ios6


    【解决方案1】:

    这就是我添加viewController 的方式。

    我替换了这行代码:

    [window addSubview:viewController.view];
    

    通过这一行:

    [window setRootViewController:viewController];
    

    【讨论】:

    • 我吓坏了,谢谢!知道什么时候改变了吗?我的代码库已有 2 年多的历史了,可能是我用 Objective-c 编写的第一行代码。
    • 这适用于根视图控制器,但不适用于我做 pushViewController 的子视图控制器,有什么建议吗?
    • 接受答案!它对我有用 :) 假设您已将问题中的方法添加到每个方法中,您是否也尝试添加 preferredInterfaceOrientationForPresentation?
    【解决方案2】:

    当设备改变方向时,在 iOS 6 中,系统会询问应用程序它支持哪个方向。应用程序将返回它接受的多个方向。

    应用程序如何确定它的方向?

    1. 首先应用程序检查它的 info.plist 方向(这对于确定启动时使用哪个方向非常重要。
    2. 其次,它向应用程序委托询问其方向,这可以通过-(NSUInteger)application:supportedInterfaceOrientationsForWindow: 方法指定。这有效地覆盖了 info.plist 设置,此实现是可选的。默认情况下,iPad 应用面向所有方向,而 iPhone 则几乎是上下颠倒。
    3. 最后,应用程序委托查询它的最顶层视图控制器,这可以是UINavigationController 或UIViewController...等,然后它指定如何呈现以及是否要自动旋转。这些UIViewControllers 可以使用shouldAutorotate: 和supportedInterfaceOrientations 方法告诉应用委托如何呈现它。

    您必须确保设置了窗口的根视图控制器。

    此外,如果您正在全屏显示任何其他视图控制器,例如模态视图控制器,则它负责确定方向是否更改。

    【讨论】:

    • 'application:supportedInterfaceOrientationsForWindow' 的 ApplicationDelegate 方法的大 +1
    【解决方案3】:

    对我来说,解决方案有效,但情况有所不同,因为我有一个UINavigationController。 我的情况是我需要除一个之外的所有肖像窗口。我必须在目标中启用所有横向和纵向(否则它会在唯一的横向视图上崩溃)。

    所以在这种情况下:

    1. 为UINavigationController创建一个子类,
    2. 在那里插入旋转特定的东西
    3. 并使用子类而不是UINavigationController。

    【讨论】:

    【解决方案4】:

    如果您使用的是 UINavigationController,它会将 supportedInterfaceOrientations: 消息发送到 UINavigationController 本身,而不是最顶层的视图控制器(即您实际希望它执行的操作)。

    您可以简单地使用add a category to UINavigationController 来修复它,而不必将 UINavigationController 子类化。

    【讨论】:

      【解决方案5】:

      我在我的单例中创建了一个新变量:canRotate,当我希望视图控制器支持某些方向时,我将其设置为 YES。

      在 appDelegate 中我添加了这个:

      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
      {
          if([[SharedData sharedInstance] canRotate])
          {
              return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
          }
          return UIInterfaceOrientationMaskPortrait;
      }
      

      在我的情况下它有效。我有一个自定义标签栏,UINavigationviewControllers 作为子视图添加到 rootViewController。

      【讨论】:

        【解决方案6】:

        我知道这听起来很简单,但我在 iPhone 上测试时绞尽脑汁想弄清楚方向问题 - 我在纵向模式下设置了物理自动锁定模式 - 所以我在编程上所做的任何更改都无关紧要 - 认为这应该是故障排除第 1 步!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-22
          • 2012-09-15
          • 2019-12-23
          • 1970-01-01
          • 2014-02-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多