NSObject+CNLiveCurrentVC.swift
1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// NSObject+CNLiveCurrentVC.swift
// metaCode
//
// Created by open on 2023/6/8.
//
import Foundation
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)
}
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
}
}