From c0441f78533bad094a672a26533dfde3c1bfdd1f Mon Sep 17 00:00:00 2001 From: edwinQQQ Date: Sat, 11 Oct 2025 17:36:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0=20EPProgressHUD?= =?UTF-8?q?=20=E5=92=8C=20YUMIMacroUitls.h=20=E4=BB=A5=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=20iOS=2013+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 主要变更: 1. 在 EPProgressHUD.swift 中引入 keyWindow 的兼容获取方法,替换原有的 UIApplication.shared.keyWindow 调用。 2. 在 YUMIMacroUitls.h 中添加状态栏高度和 keyWindow 的兼容宏定义,确保在 iOS 13+ 中正确获取相关窗口和状态栏信息。 此更新旨在提升代码的兼容性和稳定性,确保在新版本的 iOS 中正常运行。 --- YuMi/E-P/Common/EPProgressHUD.swift | 17 ++++++++++--- YuMi/Global/YUMIMacroUitls.h | 38 ++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) 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