自动获取根控制器和windowScene

This commit is contained in:
xaoxuu 2020-06-19 10:48:47 +08:00
parent 03eebc6c93
commit 639dab2db0
6 changed files with 35 additions and 25 deletions

View File

@ -32,7 +32,7 @@
CD8BFF1B23014867001E08DD /* TestGuardVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestGuardVC.swift; sourceTree = "<group>"; };
CD8BFF1D230148DD001E08DD /* BaseListVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseListVC.swift; sourceTree = "<group>"; };
CD8BFF1F23014CB5001E08DD /* EmptyVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyVC.swift; sourceTree = "<group>"; };
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 = "<group>"; };
CDA4E03D20D3935B00CD2A0C /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
CDA4E04020D3935B00CD2A0C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@ -83,7 +83,7 @@
CDA4E03920D3935B00CD2A0C /* Products */ = {
isa = PBXGroup;
children = (
CDA4E03820D3935B00CD2A0C /* ProHUD (Xcode11之前的老项目).app */,
CDA4E03820D3935B00CD2A0C /* ProHUD 老项目.app */,
);
name = Products;
sourceTree = "<group>";
@ -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";

View File

@ -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)

View File

@ -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] {

View File

@ -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
}

View File

@ -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

View File

@ -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