Search code examples
iosswiftuinavigationcontrolleruiinterfaceorientation

supportedInterfaceOrientationsForWindow not called in iPad


I have an app which has all views in both Landscape and portrait however there are two view controllers which are in strict Portrait.

I used below method and its working well in all devices except iPad.

 func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    NSLog("Function called")
    if isShouldRotate == true{
        return UIInterfaceOrientationMask.AllButUpsideDown

    }

    return UIInterfaceOrientationMask.Portrait
}

Then I came to know about subclassing the UINavigationController however not know how to implement it.

Please let me know the steps.


Solution

  • I resolved it by subclassing the UINavigationController class!

    In App Delegate:

     func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask  {
    
        var currentViewController: UIViewController? = self.topViewController()
        if currentViewController != nil && currentViewController!.canAutoRotate() {
            return UIInterfaceOrientationMask.All
        }
    
    
    
        return UIInterfaceOrientationMask.Portrait
    }
    
    func topViewController() -> UIViewController? {
        if UIApplication.sharedApplication().keyWindow != nil
        {
            return self.topViewControllerWithRootViewController(UIApplication.sharedApplication().keyWindow!.rootViewController!)
        }
        return nil
    }
    
    func topViewControllerWithRootViewController(rootViewController: UIViewController?) -> UIViewController? {
        if rootViewController == nil {
            return nil
        }
        if rootViewController!.isKindOfClass(UITabBarController) {
            var tabBarController: UITabBarController = (rootViewController as? UITabBarController)!
            return self.topViewControllerWithRootViewController(tabBarController.selectedViewController)
        }
        else {
            if rootViewController!.isKindOfClass(UINavigationController) {
                var navigationController: UINavigationController = (rootViewController as? UINavigationController)!
                return self.topViewControllerWithRootViewController(navigationController.visibleViewController)
            }
            else {
                if (rootViewController!.presentedViewController != nil) {
                    var presentedViewController: UIViewController = rootViewController!.presentedViewController!
                    return self.topViewControllerWithRootViewController(presentedViewController)
                }
                else {
                    return rootViewController
                }
            }
        }
    }
    

    In the particular View Controller where you want not to rotate:

    override func canAutoRotate() -> Bool {
        return false
      }