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

时间:2017-08-24 08:41:51

标签: swift3 iso

我正在开发一个iOS应用程序,其中所有ViewControllers仅支持Portrait方向,但一个ViewController除外,它支持Portrait和landscape。我为该ViewController尝试此代码

 override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeLeft
}

但这没有用,任何人都可以帮忙吗? 感谢

1 个答案:

答案 0 :(得分:0)

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更改为您的视图控制器

相关问题