【问题标题】:Allow all orientations for only one view controller只允许一个视图控制器的所有方向
【发布时间】:2015-06-21 17:48:26
【问题描述】:

我知道这个问题已经在hereherehere 中提出。我尝试了所有可能的方法,但没有成功。

我正在尝试在 Swift (iOS 8) 中开发一个应用程序,其中只有一个视图控制器可以在所有方向上查看,其他视图控制器只能纵向查看。

为此,我在 Target->General->Device Orientation 中只启用了Portrait 模式(因为我希望应用程序只在纵向模式下运行,除了一个视图控制器)。

我已将以下代码添加到要在所有方向上操作的视图控制器中:

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.All.rawValue)
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.LandscapeLeft;
}

override func shouldAutorotate() -> Bool {
    return true;
}

但无法在横向模式下旋转它。

【问题讨论】:

    标签: ios xcode swift orientation uiinterfaceorientation


    【解决方案1】:

    执行以下操作:

    1. 在常规信息屏幕中启用所需的方向
    2. 在每个 ViewController 中,使用 true 或 false 覆盖自动旋转功能
      true 代表应该旋转的那个,false 代表其他):

      override var shouldAutorotate: Bool {
          return true
      }
      

    希望有帮助:)

    【讨论】:

    • overrideviewWillAppear 内自动旋转?这怎么可能?
    • Thaaaaat 是对的。好吧,让我解决这个问题。感谢您指出这一点?
    • 我现在不在我的 Mac 上,所以我无法检查,在 into.plist 中是否有类似 ViewController-Based Orientation 的内容? (就像状态栏一样)如果是这样,请确保将其设置为 true
    • 我成功了!你是对的,它在info.plist 中,但不是ViewController-Based Orientation 它是Supported interface orientations iPhone 和 iPad。这不是我想要的,但这部分解决了我的问题。
    【解决方案2】:

    在 viewWillAppear 中,在你想要的所有方向的特定 viewController 中试试这个:

     let value = UIInterfaceOrientationMask.All.rawValue
    
     UIDevice.currentDevice().setValue(value, forKey: "orientation") 
    

    别忘了在 Target->General->Device Orientation 中允许方向

    试试看

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      相关资源
      最近更新 更多