extension添加提示
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user