How to lock orientation of one view controller to portrait mode only in Swift

后端 未结 16 2263
梦谈多话
梦谈多话 2020-11-22 12:07

Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController.

e.g. assume it was Tabbed Application and when Si

16条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:25

    This is a generic solution for your problem and others related.

    1. Create auxiliar class UIHelper and put on the following methods:

        /**This method returns top view controller in application  */
        class func topViewController() -> UIViewController?
        {
            let helper = UIHelper()
            return helper.topViewControllerWithRootViewController(rootViewController: UIApplication.shared.keyWindow?.rootViewController)
        }
    
        /**This is a recursive method to select the top View Controller in a app, either with TabBarController or not */
        private func topViewControllerWithRootViewController(rootViewController:UIViewController?) -> UIViewController?
        {
            if(rootViewController != nil)
            {
                // UITabBarController
                if let tabBarController = rootViewController as? UITabBarController,
                    let selectedViewController = tabBarController.selectedViewController {
                    return self.topViewControllerWithRootViewController(rootViewController: selectedViewController)
                }
    
                // UINavigationController
                if let navigationController = rootViewController as? UINavigationController ,let visibleViewController = navigationController.visibleViewController {
                    return self.topViewControllerWithRootViewController(rootViewController: visibleViewController)
                }
    
                if ((rootViewController!.presentedViewController) != nil) {
                    let presentedViewController = rootViewController!.presentedViewController;
                    return self.topViewControllerWithRootViewController(rootViewController: presentedViewController!);
                }else
                {
                    return rootViewController
                }
            }
    
            return nil
        }
    

    2. Create a Protocol with your desire behavior, for your specific case will be portrait.

    protocol orientationIsOnlyPortrait {}

    Nota: If you want, add it in the top of UIHelper Class.

    3. Extend your View Controller

    In your case:

    class Any_ViewController: UIViewController,orientationIsOnlyPortrait {
    
       ....
    
    }
    

    4. In app delegate class add this method:

     func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
            let presentedViewController = UIHelper.topViewController()
            if presentedViewController is orientationIsOnlyPortrait {
                return .portrait
            }
            return .all
        }
    

    Final Notes:

    • If you that more class are in portrait mode, just extend that protocol.
    • If you want others behaviors from view controllers, create other protocols and follow the same structure.
    • This example solves the problem with orientations changes after push view controllers

提交回复
热议问题