【问题标题】:How to prevent rotation on iOS 11?如何防止 iOS 11 上的旋转?
【发布时间】:2018-02-18 16:50:24
【问题描述】:

从 iOS 11.2 开始,我注意到 (BOOL)shouldAutorotate, (BOOL)shouldAutorotateToInterfaceOrientation, (UIInterfaceOrientationMask)supportedInterfaceOrientations 等不再被调用。

我现在如何防止旋转到某个方向?

我可以在目标的部署级别关闭轮换,但我真正希望它为 iPhone X 关闭轮换只是。我有一个没有情节提要的较旧应用程序,并且不要不想更改应用程序以在横向模式下尊重屏幕上的愚蠢缺口。

横向在我的应用程序中很有用,因为在 iPhone 上,横向模式下您可以获得更宽的键盘,这很好。否则,我将删除所有 iPhone 型号的旋转..

在UIViewController的文档中:

“从 iOS 8 开始,所有与旋转相关的方法都已弃用。相反,旋转被视为视图控制器视图大小的变化,因此报告使用...”

和:

“您可以覆盖preferredInterfaceOrientationForPresentation..”

但这也没有被调用。不在根视图控制器中,也不在其他视图控制器中。

那么,我现在如何以编程方式防止旋转到某个方向?

【问题讨论】:

    标签: rotation orientation ios11


    【解决方案1】:

    如documentation中所述

    视图控制器可以覆盖supportedInterfaceOrientations 方法来限制支持的方向列表。

    所以我们需要覆盖shouldAutorotate 和supportedInterfaceOrientation 来定位view controllers。

    通常,系统仅在窗口的根视图控制器或呈现为填充整个屏幕的视图控制器上调用此方法。

    如果您有非常简单的配置,例如您的目标 view controller 是 rootViewController 或 window 或被呈现为覆盖整个屏幕,这将起作用。

    但是,当您有其他配置(例如 target view controller)嵌入到其他容器视图控制器(例如 UINavigationController 或 UITabBarController)中时,现在应用程序的窗口只会向其 rootViewController(现在为 container view controller)询问 @ 987654334@。

    现在进一步阅读文档,

    子视图控制器使用其父视图控制器为它们提供的窗口部分,不再直接参与有关支持哪些旋转的决策。

    因此,这些容器视图控制器的默认实现可能不会向那里的孩子询问 supportedInterfaceOrientation 偏好。

    所以为了让我们的目标child view controller 在那里指定supportedIntefaceOrientation,我们需要告诉那里的容器视图控制器这样做。

    如果是UINavigationController

    extension UINavigationController {
      open override var shouldAutorotate: Bool {
        return true
      }
    
      open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return topViewController?.supportedInterfaceOrientations ?? .allButUpsideDown
      }
    }
    

    万一还是UITabBarController

    extension UITabBarController {
      open override var shouldAutorotate: Bool {
          return true
      }
    
      open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return selectedViewController?.supportedInterfaceOrientations ?? .allButUpsideDown
      }
    }
    

    编辑

    要在视图控制器中实现正确的旋转行为,您应该将这些 ContainerViewController(UINavigationController、UITabBarController、UISplitViewController 等)子类化,然后覆盖这些属性,因为扩展这些UIKit 类全局 将导致Customizing Existing Classes 中提到的意外行为。

    如果一个类别中声明的方法的名称与原始类中的方法名称相同,或者同一类(甚至超类)上的另一个类别中的方法名称相同,则行为未定义至哪个方法实现在运行时使用。如果您在自己的类中使用类别,这不太可能成为问题,但在使用类别向标准 Cocoa 或 Cocoa Touch 类添加方法时可能会导致问题。

    【讨论】:

      猜你喜欢
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 2013-03-09
      • 2011-02-13
      • 2011-05-20
      • 1970-01-01
      相关资源
      最近更新 更多