美文网首页
Swift 获取当前显示的ViewController

Swift 获取当前显示的ViewController

作者: 花漾爱意 | 来源:发表于2020-12-14 10:56 被阅读0次

    Swift 获取当前显示的ViewController

    class public func getCurrentVc() -> UIViewController{
        let rootVc = UIApplication.shared.keyWindow?.rootViewController
        let currentVc =getCurrentVcFrom(rootVc!)
        return currentVc
       }
    
    class private func getCurrentVcFrom(_ rootVc:UIViewController) -> UIViewController{
      var currentVc:UIViewController
      var rootCtr = rootVc
      if(rootCtr.presentedViewController != nil) {
        rootCtr = rootVc.presentedViewController!
      }
      if rootVc.isKind(of:UITabBarController.classForCoder()) {
        currentVc = getCurrentVcFrom((rootVc as! UITabBarController).selectedViewController!)
      }else if rootVc.isKind(of:UINavigationController.classForCoder()){
        currentVc = getCurrentVcFrom((rootVc as! UINavigationController).visibleViewController!)
      }else{
        currentVc = rootCtr
      }
      return currentVc
    }
    

    相关文章

      网友评论

          本文标题:Swift 获取当前显示的ViewController

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