仅在某些特定的ViewController中启用横向

时间:2019-05-28 02:33:42

标签: ios swift

我的应用程序有许多不同的ViewController,我想允许某些特定的ViewController可以旋转风景。我尝试了某种方法,例如使用supportedInterfaceOrientations或shouldAutorotate,但是它不起作用。 还有其他想法吗?

1 个答案:

答案 0 :(得分:1)

在ViewController中更新还不够,您必须在AppDelegate中进行更多更新。

AppDelegate

var enableAllOrientation = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if enableAllOrientation {
        return .all
    }
    return .portrait
}

要支持横向的任何ViewController,只需实现:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.enableAllOrientation = true
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.enableAllOrientation = false
}