iOS Swift获取当前显示的ViewController

2023-01-05  本文已影响0人  Lee坚武
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
}

本文由mdnice多平台发布

上一篇 下一篇

猜你喜欢

热点阅读