// 获取当前显示的ViewController
extension UIViewController
{
// 获取当前显示的ViewController
class func currentViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
{
if let nav = base as? UINavigationController
{
return currentViewController(base: nav.visibleViewController)
}
if let tab = base as? UITabBarController
{
return currentViewController(base: tab.selectedViewController)
}
if let presented = base?.presentedViewController
{
return currentViewController(base: presented)
}
return base
}
}
获取当前VC:
let theViewControllerYouSee = UIViewController.currentViewController()
PS:转载仅为了学习使用,如有不妥,请联系,即删。