Swift|Swift - 判断指定 ViewController是否显示

// 获取当前显示的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 } }

【Swift|Swift - 判断指定 ViewController是否显示】// 比较指定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 }

    推荐阅读