对于UINavigationController和UIViewController,在swift 4中禁用屏幕方向

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对于UINavigationController和UIViewController,在swift 4中禁用屏幕方向相关的知识,希望对你有一定的参考价值。

嗨,我试图在我的项目中的一个视图控制器中停止自动旋转。在下面的快照中,enter image description here

我有一个起始的UINavigationController然后是UIViewController。我已经实现了以下代码来停止自动旋转:

extension UINavigationController {

    override open var shouldAutorotate: Bool {
        get {
            return false
        }
    }

    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        get {
            return UIInterfaceOrientationMask.landscape
        }
    }}

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
       // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override open var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask
    {
        return .landscape
    }
  }

但上面的代码似乎没有调用,也没有产生任何影响。我正在使用Xcode 9swift 4.0。任何建议将不胜感激。

此致,neena

答案

创建一个类并将其设置为UINavigationController,然后在Storyboard / XIB中使用这个新类

在您的扩展中:

class NavigationController: UINavigationController {     
    override var shouldAutorotate: Bool {
        return topViewController?.shouldAutorotate ?? super.shouldAutorotate
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return topViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
    }
}

在你的控制器中:

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}
另一答案

我遇到了同样的问题。我查看堆栈溢出并尝试了许多解决方案,但没有一个工作。我在苹果开发者论坛上找到了这个解决方案,它就像一个魅力。

我在这里添加他们的解决方案,希望将来更容易找到其他人,将author归功于their post,并链接到func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return self.orientationLock } struct AppUtility { static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { if let delegate = UIApplication.shared.delegate as? AppDelegate { delegate.orientationLock = orientation } } static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) { self.lockOrientation(orientation) UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") } }

添加到AppDelegate文件:

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

    AppDelegate.AppUtility.lockOrientation(.portrait)

}

override func viewWillDisappear(_ animated: Bool) {

    super.viewWillDisappear(animated)

    AppDelegate.AppUtility.lockOrientation(.all)

}

添加到要强制方向的viewcontroller:

qazxswpoi

以上是关于对于UINavigationController和UIViewController,在swift 4中禁用屏幕方向的主要内容,如果未能解决你的问题,请参考以下文章

UINavigationController 中的 UITabBarController 适用于 iOS 8 但不适用于 7

UINavigationController

UITabBarController 内的 UINavigationController 视觉问题

带有主 UINavigationController 和详细 UINavigationController 的 UISplitViewcontroller

[UINavigationController setView] 和 [UINavigationController setViewControllers] 的区别

UINavigationController 标题和按钮