美文网首页
UIApplication扩展获取顶层控制器

UIApplication扩展获取顶层控制器

作者: 白色天空729 | 来源:发表于2018-07-12 17:56 被阅读17次
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)

相关文章

网友评论

      本文标题:UIApplication扩展获取顶层控制器

      本文链接:https://www.haomeiwen.com/subject/hvbrpftx.html