【问题标题】:Swift 3 how to lock orientationSwift 3 如何锁定方向
【发布时间】:2016-11-03 06:46:40
【问题描述】:

我正在尝试将我的viewController 锁定为纵向,而不必依赖部署信息,但我的以下代码不起作用

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

 override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
}

override func viewDidLoad() {
    super.viewDidLoad()

}

【问题讨论】:

  • 为什么不依赖这些信息呢?这不是尝试比 xcode 更聪明的好方法
  • 我复制并粘贴了您的代码,它确实将 viewController 锁定为 portrait 模式,override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return UIInterfaceOrientationMask.portrait } 应该足以满足您的需求。

标签: swift swift3 orientation


【解决方案1】:

您可以在 AppDelegate 中尝试此操作。这将锁定所有视图控制器的方向

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue)
}

【讨论】:

    【解决方案2】:

    确保将代码放在父视图控制器中,否则它可能无法正常工作,例如,在导航控制器视图控制器类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      相关资源
      最近更新 更多