// 獲取當(dāng)前顯示的ViewController
extension UIViewController
{
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
}
}
// 比較指定ViewController是否顯示在屏幕上
// AnyClass 可以使用 classForCoder()獲得
// JChatDetailViewController.classForCoder()
class func isCurrentViewControllerVisible(clzz: AnyClass) -> Bool{
let viewController = UIViewController.currentViewController()
guard viewController != nil else {
return false
}
if (viewController?.isMember(of: clzz))! {
return true
}
return false
}