美文网首页
郑州iOS點-Swift获取当前控制器最新版

郑州iOS點-Swift获取当前控制器最新版

作者: 大點哥 | 来源:发表于2022-06-30 18:26 被阅读0次
    // 当前控制器
        class func currentViewController() -> (UIViewController?) {
           var window = UIApplication.shared.keyWindow
           if window?.windowLevel != UIWindow.Level.normal{
             let windows = UIApplication.shared.windows
             for  windowTemp in windows{
               if windowTemp.windowLevel == UIWindow.Level.normal{
                  window = windowTemp
                  break
                }
              }
            }
           let vc = window?.rootViewController
           return currentVC(vc)
        }
    
        class private func currentVC(_ vc :UIViewController?) -> UIViewController? {
           if vc == nil {
              return nil
           }
           if let presentVC = vc?.presentedViewController {
              return currentVC(presentVC)
           }
           else if let tabVC = vc as? UITabBarController {
              if let selectVC = tabVC.selectedViewController {
                  return currentVC(selectVC)
               }
               return nil
            }
            else if let naiVC = vc as? UINavigationController {
               return currentVC(naiVC.visibleViewController)
            }
            else {
               return vc
            }
         }
    

    相关文章

      网友评论

          本文标题:郑州iOS點-Swift获取当前控制器最新版

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