77//
88
99#import " HudDemoViewController.h"
10+ #import < unistd.h>
1011
1112@implementation HudDemoViewController
1213
1314#pragma mark -
1415#pragma mark Lifecycle methods
1516
17+ - (void )viewDidLoad {
18+ UIView *content = [[self .view subviews ] objectAtIndex: 0 ];
19+ ((UIScrollView *)self.view ).contentSize = content.bounds .size ;
20+ }
21+
1622- (void )didReceiveMemoryWarning {
1723 [super didReceiveMemoryWarning ]; // Releases the view if it doesn't have a superview
1824 // Release anything that's not essential, such as cached data
@@ -25,6 +31,11 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
2531 return YES ;
2632}
2733
34+ - (void )didRotateFromInterfaceOrientation : (UIInterfaceOrientation)fromInterfaceOrientation {
35+ UIView *content = [[self .view subviews ] objectAtIndex: 0 ];
36+ ((UIScrollView *)self.view ).contentSize = content.bounds .size ;
37+ }
38+
2839- (void )dealloc {
2940 [super dealloc ];
3041}
@@ -33,14 +44,14 @@ - (void)dealloc {
3344#pragma mark IBActions
3445
3546- (IBAction )showSimple : (id )sender {
36- // The hud will dispable all input on the view
37- HUD = [[MBProgressHUD alloc ] initWithView: self .view];
47+ // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
48+ HUD = [[MBProgressHUD alloc ] initWithView: self .navigationController. view];
3849
3950 // HUD.graceTime = 0.5;
4051 // HUD.minShowTime = 5.0;
4152
4253 // Add HUD to screen
43- [self .view addSubview: HUD];
54+ [self .navigationController. view addSubview: HUD];
4455
4556 // Regisete for HUD callbacks so we can remove it from the window at the right time
4657 HUD.delegate = self;
@@ -50,11 +61,11 @@ - (IBAction)showSimple:(id)sender {
5061}
5162
5263- (IBAction )showWithLabel : (id )sender {
53- // The hud will dispable all input on the view
54- HUD = [[MBProgressHUD alloc ] initWithView: self .view];
64+ // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
65+ HUD = [[MBProgressHUD alloc ] initWithView: self .navigationController. view];
5566
5667 // Add HUD to screen
57- [self .view addSubview: HUD];
68+ [self .navigationController. view addSubview: HUD];
5869
5970 // Regisete for HUD callbacks so we can remove it from the window at the right time
6071 HUD.delegate = self;
@@ -66,11 +77,11 @@ - (IBAction)showWithLabel:(id)sender {
6677}
6778
6879- (IBAction )showWithDetailsLabel : (id )sender {
69- // The hud will dispable all input on the view
70- HUD = [[MBProgressHUD alloc ] initWithView: self .view];
80+ // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
81+ HUD = [[MBProgressHUD alloc ] initWithView: self .navigationController. view];
7182
7283 // Add HUD to screen
73- [self .view addSubview: HUD];
84+ [self .navigationController. view addSubview: HUD];
7485
7586 // Regisete for HUD callbacks so we can remove it from the window at the right time
7687 HUD.delegate = self;
@@ -83,14 +94,14 @@ - (IBAction)showWithDetailsLabel:(id)sender {
8394}
8495
8596- (IBAction )showWithLabelDeterminate : (id )sender {
86- // The hud will dispable all input on the view
87- HUD = [[MBProgressHUD alloc ] initWithView: self .view];
97+ // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
98+ HUD = [[MBProgressHUD alloc ] initWithView: self .navigationController. view];
8899
89100 // Set determinate mode
90101 HUD.mode = MBProgressHUDModeDeterminate;
91102
92103 // Add HUD to screen
93- [self .view addSubview: HUD];
104+ [self .navigationController. view addSubview: HUD];
94105
95106 // Regisete for HUD callbacks so we can remove it from the window at the right time
96107 HUD.delegate = self;
@@ -101,12 +112,38 @@ - (IBAction)showWithLabelDeterminate:(id)sender {
101112 [HUD showWhileExecuting: @selector (myProgressTask ) onTarget: self withObject: nil animated: YES ];
102113}
103114
115+ - (IBAction )showWithCustomView : (id )sender {
116+ // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
117+ HUD = [[MBProgressHUD alloc ] initWithView: self .navigationController.view];
118+
119+ // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
120+ // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
121+ HUD.customView = [[[UIImageView alloc ] initWithImage: [UIImage imageNamed: @" 37x-Checkmark.png" ]] autorelease ];
122+
123+ // Set custom view mode
124+ HUD.mode = MBProgressHUDModeCustomView;
125+
126+ // Add HUD to screen
127+ [self .navigationController.view addSubview: HUD];
128+
129+ // Regisete for HUD callbacks so we can remove it from the window at the right time
130+ HUD.delegate = self;
131+
132+ HUD.labelText = @" Completed" ;
133+
134+ // This would only show the completed text with no visible custom view
135+ // HUD.customView = [[UIView alloc] initWithFrame:CGRectZero];
136+
137+ // Show the HUD while the provided method executes in a new thread
138+ [HUD showWhileExecuting: @selector (myProgressTask ) onTarget: self withObject: nil animated: YES ];
139+ }
140+
104141- (IBAction )showWithLabelMixed : (id )sender {
105- // The hud will dispable all input on the view
106- HUD = [[MBProgressHUD alloc ] initWithView: self .view];
142+ // The hud will dispable all input on the view (use the higest view possible in the view hierarchy)
143+ HUD = [[MBProgressHUD alloc ] initWithView: self .navigationController. view];
107144
108145 // Add HUD to screen
109- [self .view addSubview: HUD];
146+ [self .navigationController. view addSubview: HUD];
110147
111148 // Regisete for HUD callbacks so we can remove it from the window at the right time
112149 HUD.delegate = self;
@@ -117,6 +154,41 @@ - (IBAction)showWithLabelMixed:(id)sender {
117154 [HUD showWhileExecuting: @selector (myMixedTask ) onTarget: self withObject: nil animated: YES ];
118155}
119156
157+ - (IBAction )showUsingBlocks : (id )sender {
158+ dispatch_async (dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_LOW, 0 ), ^{
159+ // Show the HUD in the main tread
160+ dispatch_async (dispatch_get_main_queue (), ^{
161+ // No need to hod onto (retain)
162+ MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo: self .navigationController.view animated: YES ];
163+ hud.labelText = @" Loading" ;
164+ });
165+
166+ // Do a taks in the background
167+ [self myTask ];
168+
169+ // Hide the HUD in the main tread
170+ dispatch_async (dispatch_get_main_queue (), ^{
171+ [MBProgressHUD hideHUDForView: self .navigationController.view animated: YES ];
172+ });
173+ });
174+ }
175+
176+ - (IBAction )showOnWindow : (id )sender {
177+ // The hud will dispable all input on the view
178+ HUD = [[MBProgressHUD alloc ] initWithView: self .view.window];
179+
180+ // Add HUD to screen
181+ [self .view.window addSubview: HUD];
182+
183+ // Regisete for HUD callbacks so we can remove it from the window at the right time
184+ HUD.delegate = self;
185+
186+ HUD.labelText = @" Loading" ;
187+
188+ // Show the HUD while the provided method executes in a new thread
189+ [HUD showWhileExecuting: @selector (myTask ) onTarget: self withObject: nil animated: YES ];
190+ }
191+
120192#pragma mark -
121193#pragma mark Execution code
122194
@@ -155,6 +227,12 @@ - (void)myMixedTask {
155227 HUD.mode = MBProgressHUDModeIndeterminate;
156228 HUD.labelText = @" Cleaning up" ;
157229 sleep (2 );
230+ // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
231+ // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
232+ HUD.customView = [[[UIImageView alloc ] initWithImage: [UIImage imageNamed: @" 37x-Checkmark.png" ]] autorelease ];
233+ HUD.mode = MBProgressHUDModeCustomView;
234+ HUD.labelText = @" Completed" ;
235+ sleep (2 );
158236}
159237
160238#pragma mark -
0 commit comments