diff --git a/Sources/UIViewController+TopMostViewController.swift b/Sources/UIViewController+TopMostViewController.swift index ec628da..37dd9f1 100644 --- a/Sources/UIViewController+TopMostViewController.swift +++ b/Sources/UIViewController+TopMostViewController.swift @@ -26,7 +26,7 @@ extension UIViewController { /// Returns the current application's top most view controller. class func topMostViewController() -> UIViewController? { - let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController + let rootViewController = UIApplication.sharedApplication().windows.first?.rootViewController return self.topMostViewControllerOfViewController(rootViewController) } @@ -49,6 +49,13 @@ extension UIViewController { return self.topMostViewControllerOfViewController(presentedViewController) } + // child view controller + for subview in viewController?.view?.subviews ?? [] { + if let childViewController = subview.nextResponder() as? UIViewController { + return self.topMostViewControllerOfViewController(childViewController) + } + } + return viewController }