郑州iOS點-Swift获取当前控制器最新版
2022-06-30 本文已影响0人
大點哥
// 当前控制器
class func currentViewController() -> (UIViewController?) {
var window = UIApplication.shared.keyWindow
if window?.windowLevel != UIWindow.Level.normal{
let windows = UIApplication.shared.windows
for windowTemp in windows{
if windowTemp.windowLevel == UIWindow.Level.normal{
window = windowTemp
break
}
}
}
let vc = window?.rootViewController
return currentVC(vc)
}
class private func currentVC(_ vc :UIViewController?) -> UIViewController? {
if vc == nil {
return nil
}
if let presentVC = vc?.presentedViewController {
return currentVC(presentVC)
}
else if let tabVC = vc as? UITabBarController {
if let selectVC = tabVC.selectedViewController {
return currentVC(selectVC)
}
return nil
}
else if let naiVC = vc as? UINavigationController {
return currentVC(naiVC.visibleViewController)
}
else {
return vc
}
}