refactor: 更新 EPProgressHUD 和 YUMIMacroUitls.h 以兼容 iOS 13+
主要变更: 1. 在 EPProgressHUD.swift 中引入 keyWindow 的兼容获取方法,替换原有的 UIApplication.shared.keyWindow 调用。 2. 在 YUMIMacroUitls.h 中添加状态栏高度和 keyWindow 的兼容宏定义,确保在 iOS 13+ 中正确获取相关窗口和状态栏信息。 此更新旨在提升代码的兼容性和稳定性,确保在新版本的 iOS 中正常运行。
This commit is contained in:
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user