MBProgressHUD
前言: 日常開發中,常需要對 MBProgressHUD 進行擴展。
MBProgressHUD 常用屬性
bezelView:
The view containing the labels and indicator (or customView) .也就是你平時看到的那個 view,用於顯示提示信息。
bezelView -> style:
可以通過設置 style 來改變提示框的樣式。
- 毛玻璃樣式(默認)
- 設置 bezelView 的背景色為 clear 來實現完全的毛玻璃
- 普通樣式
- 設置 style 為 MBProgressHUDBackgroundStyleSolidColor
- 這時候你可以通過設置 bezelView 的背景色為 clear 來實現無背景效果
backgroundView:
在 bezelView 之下。整個 HUD 覆蓋的區域,默認鋪滿添加 HUD 的 view。最好不要去設置它的 frame 。
因為 backgroundView 的 userInterfaceEnabled 默認是 YES, 所以當 HUD 顯示時,被覆蓋的 view 無法響應用戶。如果需要這時候也可以響應用戶,設置 HUD 的 userInterfaceEnabled 屬性為 NO 。
userInterfaceEnabled:
即 backgroundView 中提到的 userInterfaceEnabled。
mode:
HUD 的結構。常使用 MBProgressHUDModeCustomView 。
offset:
bezelView 的 center 相對[添加 HUD 的 view 的 center]的偏移量。
margin:
提示信息的控件在 bezelView 上的邊距,默認是 20 。
removeFromSuperViewOnHide:
當 HUD hidden 時,是否從 superView 上移除,默認是 NO,一般設置為 YES。
label、detailLabel、customView、button:
bezelView 上用於顯示提示信息的 view。
MBProgressHUD 個人常用擴展
.h
#import <MBProgressHUD/MBProgressHUD.h>
@interface MBProgressHUD (ChEx)
/**
* 展示短暫的提示框
*
* @param message 提示語
* @param image 提示圖片
* @param vc 用來顯示提示的 vc。如果 vc 為 nil 那么將顯示在 window 上
*/
+ (void) chExShowTipHUDWithMsg:(NSString *)message image:(UIImage *)image toVc:(UIViewController *)vc;
/**
* 展示帶有 indicator 的提示框(需要手動結束)
*
* @param message 提示語
* @param vc 用來顯示提示的 vc。如果 vc 為 nil 那么將顯示在 window 上
*/
+ (void) chExShowWithIndicatorMsg:(NSString *)message toVc:(UIViewController *)vc;
/**
* 展示沒有 indicator 的提示框(需要手動結束)
*
* @param message 提示語
* @param vc 用來顯示提示的 vc。如果 vc 為 nil 那么將顯示在 window 上
*/
+ (void) chExShowMsg:(NSString *)message toVc:(UIViewController *)vc;
/**
* 移除 vc.view 上的 HUD
*
* @param vc 如果 vc 為 nil ,那么將會移除 window 上的 HUD
*/
+ (void) chExHiddenHUDForVc:(UIViewController *)vc;
@end
.m
#import "MBProgressHUD+ChEx.h"
static NSString *const kHudMagin = @"5";
static NSString *const kHudShowTimerval = @"1.5";
static NSString *const kHudFontSize = @"15";
#define HUD_FRAME CGRectMake(0, 128, CH_SCREEN_WIDTH, CH_SCREEN_HEIGHT - 128)
@implementation MBProgressHUD (ChEx)
/**
* 展示短暫的提示框
*
* @param message 提示語
* @param image 提示圖片
* @param vc 用來顯示提示的 vc。如果 vc 為 nil 那么將顯示在 window 上
*/
+ (void) chExShowTipHUDWithMsg:(NSString *)message image:(UIImage *)image toVc:(UIViewController *)vc {
CH_MAIN_BLOCK(^{
MBProgressHUD *hud;
if (vc.view) {
hud = [[MBProgressHUD alloc]initWithView:vc.view];
[vc.view addSubview:hud];
if (vc.edgesForExtendedLayout == 0) {
hud.offset = CGPointMake(0, -32);
}
}
else {
hud = [[MBProgressHUD alloc] initWithFrame:HUD_FRAME];
[[self lastWindow] addSubview:hud];
hud.offset = CGPointMake(0, -64);
}
hud.mode = MBProgressHUDModeCustomView;
[self styleWithHUD:hud];
if (image) {
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
hud.customView = imageView;
}
if (message) {
hud.label.font = [UIFont systemFontOfSize:kHudFontSize.integerValue];
hud.label.text = message;
}
[hud showAnimated:YES];
[hud hideAnimated:YES afterDelay:kHudShowTimerval.floatValue];
});
}
/**
* 展示帶有 indicator 的提示框(需要手動結束)
*
* @param message 提示語
* @param vc 用來顯示提示的 vc。如果 vc 為 nil 那么將顯示在 window 上
*/
+ (void) chExShowWithIndicatorMsg:(NSString *)message toVc:(UIViewController *)vc {
[self chExShowHUDWithMsg:message toVc:vc mode:MBProgressHUDModeIndeterminate];
}
/**
* 展示沒有 indicator 的提示框(需要手動結束)
*
* @param message 提示語
* @param vc 用來顯示提示的 vc。如果 vc 為 nil 那么將顯示在 window 上
*/
+ (void) chExShowMsg:(NSString *)message toVc:(UIViewController *)vc {
[self chExShowHUDWithMsg:message toVc:vc mode:MBProgressHUDModeCustomView];
}
+ (void) chExShowHUDWithMsg:(NSString *)message toVc:(UIViewController *)vc mode:(MBProgressHUDMode)mode {
CH_MAIN_BLOCK(^{
MBProgressHUD *hud;
if (vc.view) {
hud = [[MBProgressHUD alloc]initWithView:vc.view];
[vc.view addSubview:hud];
if (vc.edgesForExtendedLayout == 0) {
hud.offset = CGPointMake(0, -32);
}
}
else {
hud = [[MBProgressHUD alloc] initWithFrame:HUD_FRAME];
[[self lastWindow] addSubview:hud];
hud.offset = CGPointMake(0, -64);
}
hud.mode = mode;
[self styleWithHUD:hud];
if (message) hud.label.text = message;
[hud showAnimated:YES];
});
}
+ (void) chExHiddenHUDForVc:(UIViewController *)vc {
CH_MAIN_BLOCK(^{
if (vc.view)
[self hideHUDForView:vc.view animated:YES];
else
[self hideHUDForView:[self lastWindow] animated:YES];
});
}
#pragma mark
#pragma mark pravite method
+ (void) styleWithHUD:(MBProgressHUD *)hud {
hud.margin = kHudMagin.integerValue;
hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
hud.bezelView.backgroundColor = [UIColor redColor];
hud.removeFromSuperViewOnHide = YES;
}
+ (UIView *) lastWindow {
return [[UIApplication sharedApplication].windows lastObject];
}
@end
說明:
