How to maintain presenting view controller's orientation when dismissing modal view controller?

前端 未结 9 2135
礼貌的吻别
礼貌的吻别 2020-11-30 03:57

I have this app I am working on and I need ALL my view controllers but one to be in portrait. The single one view controller that is special I need it to be able to rotate t

9条回答
  •  野性不改
    2020-11-30 04:35

    Swift 3.0 OR Above, Just check "isBeingDismissed" property of presented view controller. Below is sample code, This is will rotate presenting view controller to portrait mode immediately after presented view controller is dismissed.

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController)
    {
      if rootViewController.canRotateVC == true
      {
        if baseVC.isBeingDismissed == false
        {
          return .allButUpsideDown
        }
      }
    }
    
      return .portrait}
    

    you can get topController by below code:

      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 }
    

提交回复
热议问题