MBProgressHUD

https://github.com/jdg/MBProgressHUD

MBProgressHUD 是目前Github上star最多的HUD,支持所有版本的iOS,但是不支持Mac。

效果图

安装

Cocoapods

Podfile里面添加

1
pod 'MBProgressHUD', '~> 0.9.2'

或者:

Carthage

Cartfile里面添加

1
github "jdg/MBProgressHUD" ~> 0.9.2

或者:

Source files

直接把 MBProgressHUD.hMBProgressHUD.m添加到工程中。

或者:

Static library

MBProgressHUD.xcodeproj拖到工程文件中,Libraries中添加libMBProgressHUD.a,再将MBProgressHUD添加到Target Dependencies list里面。

使用

标准情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];

HUD.delegate = self;
HUD.labelText = @"Loading";
HUD.detailsLabelText = @"updating data";
HUD.square = YES;

[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];

- (void)myTask {
// Do something usefull in here instead of sleeping ...
sleep(3);
}

带进度条的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];

// Set determinate mode
HUD.mode = MBProgressHUDModeAnnularDeterminate;

HUD.delegate = self;
HUD.labelText = @"Loading";

// myProgressTask uses the HUD instance to update progress
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];


- (void)myProgressTask {
// This just increases the progress indicator in a loop
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
usleep(50000);
}
}

更详细的用法可以参考官方的DEMO