<分区>
所以,我有 self
作为 UIWindow
,但是我怎样才能在当前时刻获得 visibleViewController?
<分区>
所以,我有 self
作为 UIWindow
,但是我怎样才能在当前时刻获得 visibleViewController?
最佳答案
在 swift3 中:
func getVisibleViewController(_ rootViewController: UIViewController?) -> UIViewController? {
var rootVC = rootViewController
if rootVC == nil {
rootVC = UIApplication.shared.keyWindow?.rootViewController
}
if rootVC?.presentedViewController == nil {
return rootVC
}
if let presented = rootVC?.presentedViewController {
if presented.isKind(of: UINavigationController.self) {
let navigationController = presented as! UINavigationController
return navigationController.viewControllers.last!
}
if presented.isKind(of: UITabBarController.self) {
let tabBarController = presented as! UITabBarController
return tabBarController.selectedViewController!
}
return getVisibleViewController(presented)
}
return nil
}
关于ios - 使用 UIWindow 获取 visibleViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526926/