MBProgressHUD 常用扩展

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

说明:

  • 所有接口都不会遮挡导航栏
  • 使用 UIViewController 作为参数,而没使用 UIView ,因为 iOS 7 之后视图布局由变更导航栏遮挡视图
  • 如何使用 window 来承载 HUD 却不遮挡导航栏Here
posted @ 2017-03-15 10:59  上水的花  阅读(3209)  评论(1)    收藏  举报