diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 46d6f98..25cf702 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -32,7 +32,7 @@ CD8BFF1B23014867001E08DD /* TestGuardVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestGuardVC.swift; sourceTree = ""; }; CD8BFF1D230148DD001E08DD /* BaseListVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseListVC.swift; sourceTree = ""; }; CD8BFF1F23014CB5001E08DD /* EmptyVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyVC.swift; sourceTree = ""; }; - CDA4E03820D3935B00CD2A0C /* ProHUD (Xcode11之前的老项目).app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ProHUD (Xcode11之前的老项目).app"; sourceTree = BUILT_PRODUCTS_DIR; }; + CDA4E03820D3935B00CD2A0C /* ProHUD 老项目.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ProHUD 老项目.app"; sourceTree = BUILT_PRODUCTS_DIR; }; CDA4E03B20D3935B00CD2A0C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; CDA4E03D20D3935B00CD2A0C /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; CDA4E04020D3935B00CD2A0C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -83,7 +83,7 @@ CDA4E03920D3935B00CD2A0C /* Products */ = { isa = PBXGroup; children = ( - CDA4E03820D3935B00CD2A0C /* ProHUD (Xcode11之前的老项目).app */, + CDA4E03820D3935B00CD2A0C /* ProHUD 老项目.app */, ); name = Products; sourceTree = ""; @@ -127,7 +127,7 @@ ); name = Example; productName = Example; - productReference = CDA4E03820D3935B00CD2A0C /* ProHUD (Xcode11之前的老项目).app */; + productReference = CDA4E03820D3935B00CD2A0C /* ProHUD 老项目.app */; productType = "com.apple.product-type.application"; }; /* End PBXNativeTarget section */ @@ -383,7 +383,7 @@ ); MARKETING_VERSION = 2.0; PRODUCT_BUNDLE_IDENTIFIER = com.xaoxuu.ProHUDExample; - PRODUCT_NAME = "ProHUD (Xcode11之前的老项目)"; + PRODUCT_NAME = "ProHUD 老项目"; PROVISIONING_PROFILE = "b32d51a3-c20b-408f-aebf-11d2ea3811cd"; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OBJC_BRIDGING_HEADER = "Example/Example-Bridging-Header.h"; @@ -409,7 +409,7 @@ ); MARKETING_VERSION = 2.0; PRODUCT_BUNDLE_IDENTIFIER = com.xaoxuu.ProHUDExample; - PRODUCT_NAME = "ProHUD (Xcode11之前的老项目)"; + PRODUCT_NAME = "ProHUD 老项目"; PROVISIONING_PROFILE = "b32d51a3-c20b-408f-aebf-11d2ea3811cd"; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OBJC_BRIDGING_HEADER = "Example/Example-Bridging-Header.h"; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 7c96ad9..2e17f46 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -22,14 +22,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { window = UIWindow.init(frame: UIScreen.main.bounds) window?.rootViewController = vc window?.makeKeyAndVisible() - ProHUD.config { (cfg) in - cfg.rootViewController = vc - if #available(iOS 13.0, *) { - cfg.windowScene = window?.windowScene - } else { - // Fallback on earlier versions - } + // 可自动获取根控制器,如果获取失败请主动设置此值 + // cfg.rootViewController = vc cfg.alert { (a) in a.titleFont = .bold(22) a.bodyFont = .regular(17) diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index d4a572f..373d23f 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -15,7 +15,7 @@ class ViewController: BaseListVC { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. - title = "ProHUD" + title = "\(Bundle.main.infoDictionary?["CFBundleName"] ?? "ProHUD")" } override var titles: [String] { diff --git a/Source/Alert/AlertController.swift b/Source/Alert/AlertController.swift index 7f048d8..35fd21d 100644 --- a/Source/Alert/AlertController.swift +++ b/Source/Alert/AlertController.swift @@ -95,7 +95,7 @@ public extension Alert { window.resignKey() window.addSubview(view) if #available(iOS 13.0, *) { - window.windowScene = cfg.windowScene + window.windowScene = cfg.windowScene ?? UIApplication.shared.windows.last?.windowScene } else { // Fallback on earlier versions } diff --git a/Source/Guard/GuardController.swift b/Source/Guard/GuardController.swift index 9f55281..c1b2547 100644 --- a/Source/Guard/GuardController.swift +++ b/Source/Guard/GuardController.swift @@ -99,7 +99,7 @@ public extension Guard { /// 推入某个视图控制器 /// - Parameter viewController: 视图控制器 @discardableResult func push(to viewController: UIViewController? = nil) -> Guard { - func f(_ vc: UIViewController) { + func f(_ vc: UIViewController) -> Guard { view.layoutIfNeeded() vc.addChild(self) vc.view.addSubview(view) @@ -114,10 +114,25 @@ public extension Guard { UIView.animateForGuard { self.privTranslateIn() } + return self } if let vc = viewController ?? cfg.rootViewController { - f(vc) + return f(vc) } else { + // 尝试获取RootVC + let ws = UIApplication.shared.windows.reversed().filter { (w) -> Bool in + // 去除掉诸如 UITextEffectsWindow 这样的类,去掉隐藏的Window + if "\(type(of:w))" == "UIWindow" && w.isHidden == false { + return true + } else { + return false + } + } + for w in ws { + if let vc = w.rootViewController { + return f(vc) + } + } debug("请传入需要push到的控制器") } return self diff --git a/Source/Toast/ToastController.swift b/Source/Toast/ToastController.swift index f01eaf7..9e0c371 100644 --- a/Source/Toast/ToastController.swift +++ b/Source/Toast/ToastController.swift @@ -109,19 +109,19 @@ public extension Toast { let config = cfg.toast let isNew: Bool if self.window == nil { - let w = UIWindow(frame: .zero) - self.window = w + let window = UIWindow(frame: .zero) + self.window = window if #available(iOS 13.0, *) { - w.windowScene = cfg.windowScene + window.windowScene = cfg.windowScene ?? UIApplication.shared.windows.last?.windowScene } else { // Fallback on earlier versions } - w.windowLevel = UIWindow.Level(5000) - w.backgroundColor = .clear - w.layer.shadowRadius = 8 - w.layer.shadowOffset = .init(width: 0, height: 5) - w.layer.shadowOpacity = 0.2 - w.isHidden = false + window.windowLevel = UIWindow.Level(5000) + window.backgroundColor = .clear + window.layer.shadowRadius = 8 + window.layer.shadowOffset = .init(width: 0, height: 5) + window.layer.shadowOpacity = 0.2 + window.isHidden = false isNew = true } else { isNew = false