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