ios - 如何只为一个 Controller 解锁方向?

标签 ios iphone swift screen-orientation

如何仅解锁一个 Controller 的方向?我的整个应用程序只允许使用一种纵向模式,但仅在一种模式下我需要横向模式。为此,我尝试了下一个片段:

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

override func shouldAutorotate() -> Bool{
    return false
}

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

但这对我没有帮助。还有其他解决办法吗?

最佳答案

在Appdelegate中定义一个var:

import UIKit
var isViewAppeared = false

现在在 Appdelegate 中:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if isViewAppeared{
      return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
    }
    return Int(UIInterfaceOrientationMask.Portrait.rawValue)
  }

现在在 ViewController 中您想要横向的位置:

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    isViewAppeared = true

  }

  override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    isViewAppeared = false

    UIView.animateWithDuration(0.4, animations: { () -> Void in
      let value = UIInterfaceOrientation.Portrait.rawValue
      UIDevice.currentDevice().setValue(value, forKey: "orientation")
    })

  }

关于ios - 如何只为一个 Controller 解锁方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331277/

相关文章:

iphone - 如何判断两个物体是否相交?

iphone - Facebook app请求从移动网站打开应用程序

swift - 结合 flatMap/Scan 来携带中间结果

ios - CKQueryOperation 和 Perform(Fetch...) 之间的区别

ios - "absolute position" ScrollView 内的 View

ios - NSURLSessionUploadTask 返回 HTTP 403 错误页面

ios - 聚光灯搜索后显示 View Controller

iphone - 如何创建 .bundle 来存储图像?

iphone - 根据内容更改单个 UITableViewCell 的外观

ios - 如何使多个右栏按钮项目在导航栏中垂直堆叠?