extension添加提示
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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