LoginSignup
7
6

More than 5 years have passed since last update.

keyWindowの一番上にあるViewControllerにpresentViewControllerする

Posted at

ViewControllerの仕様にあわせてpresentViewController出来そうなViewControllerを探しまくってそこにpresentViewControllerする。
keyWindowのrootViewControllerはpresentされていることもあるしこうしないと常にpresentViewControllerすることができない。

let rootVC = topViewController(UIApplication.sharedApplication().keyWindow?.rootViewController)
    rootVC?.presentViewController(vc, animated: true, completion: nil)

private func topViewController(rootVC:UIViewController?)->UIViewController?{
    if let vc = rootVC as? UITabBarController{
      return topViewController(vc.selectedViewController)
    }else if let vc = rootVC as? UINavigationController {
      return topViewController(vc.visibleViewController)
    }else if let vc = rootVC?.presentedViewController {
      return topViewController(vc)
    }else {
      return rootVC
    }
  }

デバッグ画面出したりするときに使うと便利

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6