swift 获取当前viewcontroller 和 windo
extension UIApplication {
var currentWindow: UIWindow? {
if #available(iOS 13.0, *) {
if let window = connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first{
return window
}else if let window = UIApplication.shared.delegate?.window{
return window
}else{
return nil
}
} else {
if let window = UIApplication.shared.delegate?.window{
return window
}else{
return nil
}
}
}
}
extension UIViewController {
// MARK: - 找到当前显示的viewcontroller
class func current(base: UIViewController? = UIApplication.shared.currentWindow?.rootViewController) -> UIViewController? {
if let nav = base as? UINavigationController {
return current(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController {
return current(base: tab.selectedViewController)
}
if let presented = base?.presentedViewController {
return current(base: presented)
}
if let split = base as? UISplitViewController{
return current(base: split.presentingViewController)
}
return base
}
}
本文标题:swift 获取当前viewcontroller 和 windo
本文链接:https://www.haomeiwen.com/subject/iborvktx.html
网友评论