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];
- //初始化MBProgressHUD
- self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
- // self.progressHUD.mode = MBProgressHUDModeIndeterminate;
- self.progressHUD.progress = 0.4;
- //添加ProgressHUD到界面中
- [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];
- // Dispose of any resources that can be recreated.
- }
- @end
其中,MBProgressHUD有一些配置项,下面分别说明:
(1)self.progressHUD.dimBackground配置项。该项主要配置对话框是否有遮罩,取值为YES / NO,下面两张图是该配置项的区别:
(2)self.progressHUD.mode配置项。该配置项有6种不同的取值,分别对应6中不同形状的进度框,取值有下面6种:
- typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
- /** Progress is shown using an UIActivityIndicatorView. This is the default. */
- MBProgressHUDModeIndeterminate,
- /** Progress is shown using a round, pie-chart like, progress view. */
- MBProgressHUDModeDeterminate,
- /** Progress is shown using a horizontal progress bar */
- MBProgressHUDModeDeterminateHorizontalBar,
- /** Progress is shown using a ring-shaped progress view. */
- MBProgressHUDModeAnnularDeterminate,
- /** Shows a custom view */
- MBProgressHUDModeCustomView,
- /** Shows only labels */
- MBProgressHUDModeText
- };
还有一种mode为MBProgressHUDModeCustomView,即自定义的View。
隐藏进度框需要调用下面的方法:
- [self.progressHUD hide:YES];
- [self.progressHUD hide:YES afterDelay:5];
3、self.progressHUD.progress属性。该属性配置的是进度框中显示的进度,取值为0-1