// MARK: - 查找顶层控制器、
// 获取顶层控制器 根据window
func getTopVC() -> (UIViewController?) {
var window = UIApplication.shared.keyWindow
//是否为当前显示的window
if window?.windowLevel != UIWindowLevelNormal{
let windows = UIApplication.shared.windows
for windowTemp in windows{
if windowTemp.windowLevel == UIWindowLevelNormal{
window = windowTemp
break
}
}
}
let vc = window?.rootViewController
return getTopVC(withCurrentVC: vc)
}
///根据控制器获取 顶层控制器
func getTopVC(withCurrentVC VC :UIViewController?) -> UIViewController? {
if VC == nil {
print("🌶: 找不到顶层控制器")
return nil
}
if let presentVC = VC?.presentedViewController {
//modal出来的 控制器
return getTopVC(withCurrentVC: presentVC)
}else if let tabVC = VC as? UITabBarController {
// tabBar 的跟控制器
if let selectVC = tabVC.selectedViewController {
return getTopVC(withCurrentVC: selectVC)
}
return nil
} else if let naiVC = VC as? UINavigationController {
// 控制器是 nav
return getTopVC(withCurrentVC:naiVC.visibleViewController)
} else {
// 返回顶控制器
return VC
}
}
方法调用:let vc:UIViewController = getTopVC()!
判断控制器的类型
if vc .isKind(of: UserVC.classForCoder()) {
print("当前控制器是 = UserVC")
}
网友评论