瀏覽代碼

增加方法:查找当前vc

harry 3 年之前
父節點
當前提交
4824b17d61
共有 1 個文件被更改,包括 31 次插入0 次删除
  1. 31 0
      BFCommonKit/Classes/BFUtility/PQCommonMethodUtil.swift

+ 31 - 0
BFCommonKit/Classes/BFUtility/PQCommonMethodUtil.swift

@@ -15,6 +15,7 @@ import KingfisherWebP
 import Photos
 import Toast_Swift
 import RealmSwift
+import UIKit
 
 /// Home文件地址
 public let homeDirectory = NSHomeDirectory()
@@ -132,6 +133,36 @@ public func kf_imageCacheImage(originUrl: String, completeHandle: @escaping (_ i
     }
 }
 
+public func bf_getRootViewController() -> UIViewController? {
+    guard let window = UIApplication.shared.delegate?.window else {
+        return nil
+    }
+    return window!.rootViewController
+}
+
+public func bf_getCurrentViewController() -> UIViewController? {
+    var currentVC = bf_getRootViewController()
+    if currentVC == nil {
+        return nil
+    }
+    
+    let runloopFind = true
+    while runloopFind {
+        if let vc = currentVC!.presentedViewController {
+            currentVC = vc
+        }else {
+            if currentVC is UINavigationController {
+                currentVC = (currentVC as! UINavigationController).visibleViewController
+            }else if currentVC is UITabBarController {
+                currentVC = (currentVC as! UITabBarController).selectedViewController
+            }else {
+                break
+            }
+        }
+    }
+    return currentVC
+}
+
 
 /** 打印
    type = 1 : 胡志强