func getCurrentController() -> UIViewController? {
return topViewControllerWithRootViewController(viewController:getCurrentWindow()?.rootViewController)
}
func topViewController() -> UIViewController? {
return topViewControllerWithRootViewController(viewController:getCurrentController())
}
func topViewControllerWithRootViewController(viewController :UIViewController?) -> UIViewController? {
if viewController == nil {return nil}
if viewController?.presentedViewController != nil {
return topViewControllerWithRootViewController(viewController: viewController?.presentedViewController!)
}else if viewController?.isKind(of: UITabBarController.self) == true {
return topViewControllerWithRootViewController(viewController: (viewController as! UITabBarController).selectedViewController)
}else if viewController?.isKind(of: UINavigationController.self) == true {
return topViewControllerWithRootViewController(viewController: (viewController as! UINavigationController).visibleViewController)
}else{
return viewController
}
}
func getCurrentWindow() -> UIWindow? {
var window: UIWindow? = UIApplication.shared.keyWindow
if window?.windowLevel != UIWindowLevelNormal {
for tempWindow in UIApplication.shared.windows {
if tempWindow.windowLevel == UIWindowLevelNormal {
window = tempWindow
break
}
}
}
return window
}
原文转自:http://www.cnblogs.com/SoulKai/p/6278373.html
网友评论