iOS開發之MBProgressHUD的使用

來源:互聯網
上載者:User

iOS開發之MBProgressHUD的使用

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];
其中第一個方法是立即隱藏進度框,第二個方法是延遲5秒再隱藏進度框。

3、self.progressHUD.progress屬性。該屬性配置的是進度框中顯示的進度,取值為0-1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.