@@ -11,7 +11,7 @@ @implementation MBProgressHUD (MJ)
1111#pragma mark 显示信息
1212+ (void )show : (NSString *)text icon : (NSString *)icon view : (UIView *)view
1313{
14- if (view == nil ) view = [[UIApplication sharedApplication ].windows lastObject ];
14+ if (view == nil ) view = [[self get_topPresentedViewController ] view ];
1515 // 快速显示一个提示信息
1616 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo: view animated: YES ];
1717 hud.labelText = text;
@@ -27,6 +27,36 @@ + (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view
2727 [hud hide: YES afterDelay: 0.7 ];
2828}
2929
30+ + (UIViewController *)get_topPresentedViewController
31+ {
32+ UIViewController *rootVC = nil ;
33+ if (@available (iOS 13.0 , *)) {
34+ UIWindow *foundWindow = nil ;
35+ NSArray *windows = [[UIApplication sharedApplication ] windows ];
36+ for (UIWindow *window in windows) {
37+ if (window.isKeyWindow ) {
38+ foundWindow = window;
39+ break ;
40+ }
41+ }
42+ rootVC = [foundWindow rootViewController ];
43+ }
44+ else {
45+ rootVC = [UIApplication sharedApplication ].keyWindow .rootViewController ;
46+ }
47+
48+ if (!rootVC) {
49+ NSLog (@" find window rootViewController is nil!" );
50+ rootVC = [UIApplication sharedApplication ].keyWindow .rootViewController ;
51+ }
52+
53+ while (rootVC.presentedViewController ) {
54+ rootVC = rootVC.presentedViewController ;
55+ }
56+
57+ return rootVC;
58+ }
59+
3060#pragma mark 显示错误信息
3161+ (void )showError : (NSString *)error toView : (UIView *)view {
3262 [self show: error icon: @" error.png" view: view];
@@ -39,7 +69,7 @@ + (void)showSuccess:(NSString *)success toView:(UIView *)view
3969
4070#pragma mark 显示一些信息
4171+ (MBProgressHUD *)showMessage : (NSString *)message toView : (UIView *)view {
42- if (view == nil ) view = [[UIApplication sharedApplication ].windows lastObject ];
72+ if (view == nil ) view = [[ self get_topPresentedViewController ] view ];
4373 // 快速显示一个提示信息
4474 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo: view animated: YES ];
4575 hud.labelText = message;
0 commit comments