diff --git a/YuMi/E-P/Common/EPProgressHUD.swift b/YuMi/E-P/Common/EPProgressHUD.swift index 8b49666..e12059b 100644 --- a/YuMi/E-P/Common/EPProgressHUD.swift +++ b/YuMi/E-P/Common/EPProgressHUD.swift @@ -13,13 +13,25 @@ import Foundation private static var currentHUD: MBProgressHUD? + /// 获取当前活跃的 window(兼容 iOS 13+) + private static var keyWindow: UIWindow? { + if #available(iOS 13.0, *) { + return UIApplication.shared.connectedScenes + .compactMap { $0 as? UIWindowScene } + .flatMap { $0.windows } + .first { $0.isKeyWindow } + } else { + return UIApplication.shared.keyWindow + } + } + /// 显示上传进度 /// - Parameters: /// - uploaded: 已上传数量 /// - total: 总数量 @objc static func showProgress(_ uploaded: Int, total: Int) { DispatchQueue.main.async { - guard let window = UIApplication.shared.keyWindow else { return } + guard let window = keyWindow else { return } if let hud = currentHUD { // 更新现有 HUD @@ -40,8 +52,7 @@ import Foundation /// 关闭 HUD @objc static func dismiss() { DispatchQueue.main.async { - guard let window = UIApplication.shared.keyWindow, - let hud = currentHUD else { return } + guard let hud = currentHUD else { return } hud.hide(animated: true) currentHUD = nil diff --git a/YuMi/Global/YUMIMacroUitls.h b/YuMi/Global/YUMIMacroUitls.h index 84e497a..0fb5a0c 100644 --- a/YuMi/Global/YUMIMacroUitls.h +++ b/YuMi/Global/YUMIMacroUitls.h @@ -23,7 +23,19 @@ isPhoneXSeries = [[UIApplication sharedApplication] delegate].window.safeAreaIns #define KScreenWidth [[UIScreen mainScreen] bounds].size.width #define KScreenHeight [[UIScreen mainScreen] bounds].size.height -#define statusbarHeight [[UIApplication sharedApplication] statusBarFrame].size.height + +// 兼容 iOS 13+ 的状态栏高度获取 +#define statusbarHeight ({\ + CGFloat height = 0;\ + if (@available(iOS 13.0, *)) {\ + UIWindowScene *windowScene = (UIWindowScene *)[[[UIApplication sharedApplication] connectedScenes] allObjects].firstObject;\ + height = windowScene.statusBarManager.statusBarFrame.size.height;\ + } else {\ + height = [[UIApplication sharedApplication] statusBarFrame].size.height;\ + }\ + height;\ +}) + #define kStatusBarHeight statusbarHeight #define kSafeAreaBottomHeight (iPhoneXSeries ? 34 : 0) #define kSafeAreaTopHeight (iPhoneXSeries ? 24 : 0) @@ -36,8 +48,28 @@ isPhoneXSeries = [[UIApplication sharedApplication] delegate].window.safeAreaIns #define kRoundValue(value) round(kScreenScale * value) #define kWeakify(o) try{}@finally{} __weak typeof(o) o##Weak = o; #define kStrongify(o) autoreleasepool{} __strong typeof(o) o = o##Weak; -///keyWindow -#define kWindow [UIApplication sharedApplication].keyWindow + +// 兼容 iOS 13+ 的 keyWindow 获取 +#define kWindow ({\ + UIWindow *window = nil;\ + if (@available(iOS 13.0, *)) {\ + for (UIWindowScene *scene in [UIApplication sharedApplication].connectedScenes) {\ + if (scene.activationState == UISceneActivationStateForegroundActive) {\ + for (UIWindow *w in scene.windows) {\ + if (w.isKeyWindow) {\ + window = w;\ + break;\ + }\ + }\ + if (window) break;\ + }\ + }\ + } else {\ + window = [UIApplication sharedApplication].keyWindow;\ + }\ + window;\ +}) + #define kImage(image) [UIImage imageNamed:image] ///UIFont