Rotation only in one ViewController

后端 未结 13 1868
我在风中等你
我在风中等你 2020-12-05 03:20

I am trying to rotate one view while all other views (5) are fixed to portrait. The reason is that in that one view I want the user to watch pictures which he saved before.

相关标签:
13条回答
  • 2020-12-05 04:17

    You can also do it in a protocol oriented way. Just create the protocol

    protocol CanRotate {
    
    }
    

    Add the the same 2 methods in the AppDelegate in a more "swifty" way

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if topViewController(in: window?.rootViewController) is CanRotate {
            return .allButUpsideDown
        } else {
            return .portrait
        }
    }
    
    
    func topViewController(in rootViewController: UIViewController?) -> UIViewController? {
        guard let rootViewController = rootViewController else {
            return nil
        }
    
        if let tabBarController = rootViewController as? UITabBarController {
            return topViewController(in: tabBarController.selectedViewController)
        } else if let navigationController = rootViewController as? UINavigationController {
            return topViewController(in: navigationController.visibleViewController)
        } else if let presentedViewController = rootViewController.presentedViewController {
            return topViewController(in: presentedViewController)
        }
        return rootViewController
    }
    

    And in every ViewController that you want a different behaviour, just add the protocol name in the definition of the class.

    class ViewController: UIViewController, CanRotate {}
    

    If you want any particular combination, they you can add to the protocol a variable to override

    protocol CanRotate {
        var supportedInterfaceOrientations: UIInterfaceOrientationMask
    }
    
    0 讨论(0)
提交回复
热议问题