如何在iOS Swift 3中仅将一个View Controller旋转为横向 [英] How to Rotate Only One View Controller to Landscape Orientation in iOS Swift 3

查看:294
本文介绍了如何在iOS Swift 3中仅将一个View Controller旋转为横向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在此处找到更多答案: https://stackoverflow.com/a/50126802/5200447

you can find more Answers here: https://stackoverflow.com/a/50126802/5200447

推荐答案

这是针对 Swift3和Swift 4 的. 您可以在AppDelegate.swift中使用以下代码:

This is for Swift3 and Swift 4. You can use the follow code in your AppDelegate.swift :

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    guard let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController),
     (rootViewController.responds(to: Selector(("canRotate")))) else{
        // Only allow portrait (standard behaviour)
        return .portrait;
    }
    // Unlock landscape view orientations for this view controller
    return .allButUpsideDown;
}

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
    guard rootViewController != nil else { return nil }

    guard !(rootViewController.isKind(of: (UITabBarController).self)) else{
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
    }
    guard !(rootViewController.isKind(of:(UINavigationController).self)) else{
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
    }
    guard !(rootViewController.presentedViewController != nil) else{
        return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
    }
    return rootViewController
}

您可以在原始帖子中了解更多信息: http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller -to-landscape-in​​-ios-slash-swift/

you can learn more in the original post: http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in-ios-slash-swift/

这篇关于如何在iOS Swift 3中仅将一个View Controller旋转为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆