UIApplication扩展获取顶层控制器

2018-07-12  本文已影响17人  白色天空729
class func topViewController(base: UIViewController? = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        if let tab = base as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(base: selected)
            }
        }
        if let presented = base?.presentedViewController {
            return topViewController(base: presented)
        }
        return base
    }

使用:

UIApplication.topViewController()?.navigationController?.setNavigationBarHidden(false, animated: true)
上一篇 下一篇

猜你喜欢

热点阅读