在 ios、swift 3 中支持特定视图控制器的横向和纵向设备方向

Support landscape and portrait device orientation for specific view controller in ios, swift 3

我正在开发一个 iOS 应用程序,其中所有 ViewController 仅支持纵向,但一个 ViewController 除外,它同时支持纵向和横向。我尝试使用此代码 ViewController

 override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft
}

但这没有用,有人可以帮忙吗? 谢谢

在 AppDelegate 中添加此事件

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {


    if let navigationController = UIApplication.topViewController() {

        if navigationController is YOURVIEWCONTROLLERNAME  {
            return UIInterfaceOrientationMask.all
        }

        else {
            return UIInterfaceOrientationMask.portrait
        }
    }

    return UIInterfaceOrientationMask.portrait
}

extension UIApplication {
class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
        return topViewController(base: nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        if let selected = tab.selectedViewController {
            return topViewController(base: selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(base: presented)
    }
    return base
}

将 YOURVIEWCONTROLLERNAME 更改为您的视图控制器