During iOS development, users often encounter similar situations such as reading system resources. If the network is relatively slow, users may think that the app has crashed and the user experience is poor, foreigners are still very good at providing open source sources and learning from each other.
The progress bar of iOS can be divided into several categories. There are common ones, such as a circle, text under a circle, and text directly ..
Add the following two files to your project: MBProgressHUD. h and MBProgressHUD. m. Next we only need to reference the header file of progress in our. m file.
For common progress bars, the Code is as follows:
[Plain]
-(IBAction) showSimple :( id) sender {
// The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
HUD = [[MBProgressHUD alloc] initWithView: self. navigationController. view];
[Self. navigationController. view addSubview: HUD];
// Regiser for HUD callbacks so we can remove it from the window at the right time
HUD. delegate = self;
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting: @ selector (myTask) onTarget: self withObject: nil animated: YES];
}
The selector function myTask can do what we want to do:
[Plain]
-(Void) myTask {
// Do something usefull in here instead of sleeping...
Sleep (3 );
}
Effect
Progress bar with text:
[Plain]
-(IBAction) showWithLabel :( id) sender {
HUD = [[MBProgressHUD alloc] initWithView: self. navigationController. view];
[Self. navigationController. view addSubview: HUD];
HUD. delegate = self;
HUD. labelText = @ "Loading ";
[HUD showWhileExecuting: @ selector (myTask) onTarget: self withObject: nil animated: YES];
}
As follows:
Plain text code:
[Plain]
-(IBAction) showTextOnly :( id) sender {
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self. navigationController. view animated: YES];
// Configure for text only and offset down
Hud. mode = MBProgressHUDModeText;
Hud. labelText = @ "Some message ...";
Hud. margin = 10.f;
Hud. yOffset = 150.f;
Hud. removeFromSuperViewOnHide = YES;
[Hud hide: YES afterDelay: 3];
}
As follows: