0

I have a structure like this. There is a View Controller 1 embedded in a navigation controller. From it, I present a View Controller 2

Navigation Controller
|
|
|
View Controller 1 (Parent view - Portrait only)
|
|
|
View Controller 2 (Modal View - All but upside down)

The issue I'm getting is after I dismiss the Modal View which is in landscape mode, the Parent View is also in landscape mode as well. Even if I set it not auto rotate and force it into portrait mode. How to lock the Parent View orientation ?

Thanks in advance.

So far, what I have done is:

  1. Remove all the orientation mode but portrait mode in plist.

  2. Allow no controller but the Modal view to rotate

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if let rootViewController = topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
            if rootViewController.isKind(of: ViewController2.classForCoder()) {
                // Unlock landscape view orientations for this view controller
                return .allButUpsideDown;
            }
            return .portrait
        }

        // Only allow portrait (standard behaviour)
        return .portrait
    }
    
    private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
        if (rootViewController == nil) { return nil }
        if (rootViewController.isKind(of: UITabBarController.self)) {
          return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
        } else if (rootViewController.isKind(of: UINavigationController.self)) {
          return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
        } else if (rootViewController.presentedViewController != nil) {
          return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
        }
        return rootViewController
      }
  1. Delegate the orientation handle stuff to the stack controllers themself
extension UINavigationController {
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if let visibleViewController = visibleViewController {
            return visibleViewController.supportedInterfaceOrientations
        } else if let topViewController = topViewController {
            return topViewController.supportedInterfaceOrientations
        } else {
            return super.supportedInterfaceOrientations
        }
        
    }
    
    open override var shouldAutorotate: Bool {
        if let visibleViewController = visibleViewController {
            return visibleViewController.shouldAutorotate
        } else if let topViewController = topViewController {
            return topViewController.shouldAutorotate
        } else {
            return super.shouldAutorotate
        }
    }
    
    open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        if let visibleViewController = visibleViewController {
            return visibleViewController.preferredInterfaceOrientationForPresentation
        } else if let topViewController = topViewController {
            return topViewController.preferredInterfaceOrientationForPresentation
        } else {
            return .portrait
        }
    }
}

  1. Lock the orientation in the Parent View to portrait
    override var shouldAutorotate: Bool {
        return false
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }
Q.u.a.n.g L.
  • 1,265
  • 1
  • 10
  • 27
  • Check https://stackoverflow.com/questions/28938660/how-to-lock-orientation-of-one-view-controller-to-portrait-mode-only-in-swift – Visal Rajapakse Sep 15 '21 at 11:28

0 Answers0