extension添加提示

This commit is contained in:
2025-10-31 15:08:30 +08:00
parent eb0d3aaa71
commit 59d04bb33c
6 changed files with 74 additions and 36 deletions

View File

@@ -11,6 +11,7 @@
//
#import <Foundation/Foundation.h>
@class UIView; // forward declare to avoid importing UIKit in header
typedef NS_ENUM(NSUInteger, KBHUDMaskType) {
/// 不加遮罩,事件可透传到后面的视图(与 SVProgressHUDMaskTypeNone 类似)
@@ -59,6 +60,13 @@ NS_ASSUME_NONNULL_BEGIN
/// 设置自动隐藏的时长success/error/info默认 1.2s
+ (void)setAutoDismissInterval:(NSTimeInterval)interval;
/// 设置缺省承载视图App Extension 环境下必须设置,例如在键盘扩展里传入 self.view
/// 注意:内部弱引用,不会形成循环引用。
/// 若不设置,在非 Extension 的 App 内默认加到 KeyWindow在 Extension 内将不会显示。
/// 可在 viewDidLoad 或 viewDidAppear 调用一次即可。
/// @param view 作为 HUD 的承载父视图
+ (void)setContainerView:(nullable UIView *)view;
@end
NS_ASSUME_NONNULL_END

View File

@@ -5,6 +5,7 @@
#import "KBHUD.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import <UIKit/UIKit.h>
#ifndef KBSCREEN
#define KBSCREEN [UIScreen mainScreen].bounds.size
@@ -16,6 +17,7 @@ static __weak MBProgressHUD *sHUD = nil;
static KBHUDMaskType sMaskType = KBHUDMaskTypeClear; //
static BOOL sDefaultTapToDismiss = NO; //
static NSTimeInterval sAutoDismiss = 1.2;
static __weak UIView *sContainerView = nil; //
#pragma mark - Private Helpers
@@ -24,11 +26,37 @@ static NSTimeInterval sAutoDismiss = 1.2;
}
+ (MBProgressHUD *)ensureHUDWithMask:(KBHUDMaskType)mask tap:(BOOL)tap {
// 使
UIView *hostView = sContainerView;
#ifndef KB_APP_EXTENSION
// App 退 KeyWindow
if (!hostView) {
// KB_KeyWindow App PrefixHeader
UIWindow *win = nil;
// 访 UIApplication App Extension
Class uiAppClass = NSClassFromString(@"UIApplication");
if (uiAppClass && [uiAppClass respondsToSelector:@selector(sharedApplication)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
id app = [uiAppClass performSelector:@selector(sharedApplication)];
if ([app respondsToSelector:@selector(keyWindow)]) {
win = [app keyWindow];
}
if (!win && [app respondsToSelector:@selector(windows)]) {
NSArray *wins = [app windows];
win = wins.firstObject;
}
#pragma clang diagnostic pop
}
hostView = win;
}
#endif
if (!hostView) { return nil; }
MBProgressHUD *hud = sHUD;
if (!hud) {
UIWindow *win = KB_KeyWindow();
if (!win) { win = UIApplication.sharedApplication.windows.firstObject; }
hud = [MBProgressHUD showHUDAddedTo:win animated:YES];
hud = [MBProgressHUD showHUDAddedTo:hostView animated:YES];
sHUD = hud;
// 仿 SVProgressHUD
hud.removeFromSuperViewOnHide = YES;
@@ -92,6 +120,7 @@ static NSTimeInterval sAutoDismiss = 1.2;
+ (void)_showText:(NSString *)text icon:(nullable UIImage *)icon {
[self onMain:^{
MBProgressHUD *hud = [self ensureHUDWithMask:sMaskType tap:sDefaultTapToDismiss];
if (!hud) { return; }
hud.mode = icon ? MBProgressHUDModeCustomView : MBProgressHUDModeText;
hud.label.text = text ?: @"";
hud.detailsLabel.text = nil;
@@ -114,6 +143,7 @@ static NSTimeInterval sAutoDismiss = 1.2;
+ (void)showWithStatus:(NSString *)status allowTapToDismiss:(BOOL)allow {
[self onMain:^{
MBProgressHUD *hud = [self ensureHUDWithMask:sMaskType tap:allow];
if (!hud) { return; }
hud.mode = MBProgressHUDModeIndeterminate;
hud.label.text = status ?: @"";
}];
@@ -126,6 +156,7 @@ static NSTimeInterval sAutoDismiss = 1.2;
+ (void)showProgress:(float)progress status:(NSString *)status allowTapToDismiss:(BOOL)allow {
[self onMain:^{
MBProgressHUD *hud = [self ensureHUDWithMask:sMaskType tap:allow];
if (!hud) { return; }
hud.mode = MBProgressHUDModeDeterminate;
hud.progress = progress;
hud.label.text = status ?: @"";
@@ -162,4 +193,8 @@ static NSTimeInterval sAutoDismiss = 1.2;
[self showWithStatus:nil allowTapToDismiss:allow];
}
+ (void)setContainerView:(UIView *)view {
sContainerView = view;
}
@end