MBProgressHUD是iOS中的一个第三方库,主要是在界面上显示一个加载的进度框或者提示框,如下图所示:

下面就记录一下使用MBProgressHUD的方法:
1、导入MBProgressHUD到项目中
这里使用cocoapods导入,Podfile文件的内容如下:

如果不清楚MBProgressHUD的版本是多少,可以在终端下执行pod search MBProgressHUD命令,即可显示出当前的MBProgressHUD的最新版本,如下图所示:

2、在代码中使用MBProgressHUD
首先在头文件中声明一个MBProgressHUD变量,需要引入相应的头文件,ViewController.h文件的代码如下:
- #import "ViewController.h"
-
- @interface ViewController ()
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
-
- self.progressHUD.progress = 0.4;
-
- [self.view addSubview:self.progressHUD];
- }
-
- #pragma mark - 显示进度框
- -(void)showProgress:(id)sender {
- self.progressHUD.dimBackground = NO;
- self.progressHUD.labelText = @"加载中...";
- [self.progressHUD show:YES];
- }
-
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
-
- }
-
- @end
其中,MBProgressHUD有一些配置项,下面分别说明:
(1)self.progressHUD.dimBackground配置项。该项主要配置对话框是否有遮罩,取值为YES / NO,下面两张图是该配置项的区别:

(2)self.progressHUD.mode配置项。该配置项有6种不同的取值,分别对应6中不同形状的进度框,取值有下面6种:
- typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
-
- MBProgressHUDModeIndeterminate,
-
- MBProgressHUDModeDeterminate,
-
- MBProgressHUDModeDeterminateHorizontalBar,
-
- MBProgressHUDModeAnnularDeterminate,
-
- MBProgressHUDModeCustomView,
-
- MBProgressHUDModeText
- };
对应的进度框如下图所示:



还有一种mode为MBProgressHUDModeCustomView,即自定义的View。
隐藏进度框需要调用下面的方法:
- [self.progressHUD hide:YES];
- [self.progressHUD hide:YES afterDelay:5];
其中第一个方法是立即隐藏进度框,第二个方法是延迟5秒再隐藏进度框。
3、self.progressHUD.progress属性。该属性配置的是进度框中显示的进度,取值为0-1