获取当前显示的视图控制器代码:
class func topViewController(_controller:UIViewController? =UIApplication.shared.keyWindow?.rootViewController) ->UIViewController? {
if let navigationController = controller as? UINavigationController{
if let visible = navigationController.visibleViewController{
return topViewController(visible)
}}
if let tabController = controller as? UITabBarController{
if let selected = tabController.selectedViewController{
return topViewController(selected)
} }
if let presented = controller?.presentedViewController{
return topViewController(presented)
}return controller
}