【问题标题】:shouldAutoRotate not working in swiftshouldAutoRotate 不能快速工作
【发布时间】:2014-10-22 12:55:25
【问题描述】:

我知道如果 VC 有 navigationController 它应该包含一些像 this 的东西。但是迅捷呢?如何在 VC 中使用navigationController 调用shouldAutoRotate?我的navigationControllers 有标准类UINavigationController(不是自定义的)。我想为我所有的 VC 设置 shouldAutoRotate false,除了单身。据我了解,没有其他方法可以仅使用“纵向”并可以旋转单个 VC

【问题讨论】:

    标签: ios uinavigationcontroller swift rotation


    【解决方案1】:

    把它放在你的视图控制器中:

    override func shouldAutorotate() -> Bool {
    
         return false // or true for the VC that allows landscape
    }
    

    【讨论】:

    • override func shouldAutorotate() -> Bool 给出:方法不会覆盖其超类中的任何方法。删除覆盖给了我:方法 'shouldAutorotate()' 与 Objective-C 选择器 'shouldAutorotate' 与来自超类 'UIViewController' 的 'shouldAutorotate' 的 getter 冲突,具有相同的 Objective-C 选择器
    【解决方案2】:

    Swift 3 需要类型是 var 而不是 func:

    override var shouldAutorotate: Bool {
        return false
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      我发现最有效的方法是在给定的 UIViewController 上添加这个扩展 - 一个首先进入视图的扩展:

      extension UINavigationController {
      
          override open var shouldAutorotate: Bool {
              get {
                  if let visibleVC = visibleViewController {
                      return visibleVC.shouldAutorotate
                  }
                  return super.shouldAutorotate
              }
          }
      
          override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
              get {
                  if let visibleVC = visibleViewController {
                      return visibleVC.preferredInterfaceOrientationForPresentation
                  }
                  return super.preferredInterfaceOrientationForPresentation
              }
          }
      
          override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
              get {
                  if let visibleVC = visibleViewController {
                      return visibleVC.supportedInterfaceOrientations
                  }
                  return super.supportedInterfaceOrientations
              }
          }
      
      }
      

      然后你需要重写 shouldAutorotate 函数。将此添加到您的 UIViewController 类中:

      override open var shouldAutorotate: Bool {
          return false
      }
      

      您导航到的任何 UIViewController,您都可以重写 shouldAutorotate 函数并将其设置为您喜欢的任何内容(返回 true 或 false)。

      对于任何模态显示的视图,覆盖supportedInterfaceOrientations:

      override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
          return .all
      }
      

      【讨论】:

      • 这是我的确切方案,聪明的主意,谢谢 Chewie
      【解决方案4】:

      适用于 iOS13.4 模拟器 -

      import UIKit
      
      var autorotateEnabled: Bool = true
      
      public class MainViewController : UIViewController {
      
          @IBAction func lockAutoRotate(_ sender: Any) {
              autorotateEnabled = !autorotateEnabled
          }
          
          @IBAction func SetOrientationTest(_ v: UIView){
              // Change the desired orientation of this activity.
              if UIDevice.current.orientation == .portrait {
                  UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
              } else {
                  UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
              }
          }
          
          override public func viewDidLoad() {
              var savedInstanceState = UserDefaults.standard.dictionaryRepresentation()
              super.viewDidLoad()
          }
      }
      
      extension UINavigationController {
          override open var shouldAutorotate: Bool {
             return autorotateEnabled
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-21
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多