Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView headings shown on top of MBProgressHUD

So I have a subclass of UITableViewController that loads some data from the internet and uses MBProgressHUD during the loading process. I use the standard MBProgressHUD initialization.

    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];

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

    [HUD show:YES];

This is the result:

result.

Is there any way to resolve this issue, or should I just abandon MBProgressHUD?

Thanks!

like image 362
Chris Ballinger Avatar asked Aug 26 '11 03:08

Chris Ballinger


4 Answers

My solution was pretty simple. Instead of using self's view, I used self's navigationController's view.

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];

This should work for the OP because his picture shows he's using a UINavigationController. If you don't have a UINavigationController, you might add another view on top of your UITableView, and add the HUD to that. You'll have to write a little extra code to hide/show this extra view.

An unfortunate thing with this simple solution (not counting my idea adding another view mentioned above) means the user can't use the navigation controls while the HUD is showing. For my app, it's not a problem. But if you have a long running operation and the user might want to press Cancel, this will not be a good solution.

like image 200
Tyler Collier Avatar answered Oct 27 '22 05:10

Tyler Collier


It's probably because self.view is a UITableView, which may dynamically add/remove subviews including the headers, which could end up on top of the HUD after you add it as a subview. You should either add the HUD directly to the window, or (for a little more work but perhaps a better result) you could implement a UIViewController subclass which has a plain view containing both the table view and the HUD view. That way you could put the HUD completely on top of the table view.

like image 29
jtbandes Avatar answered Oct 27 '22 06:10

jtbandes


My solution was:

self.appDelegate = (kmAppDelegate *)[[UIApplication sharedApplication] delegate];
.
.
_progressHUD = [[MBProgressHUD alloc] initWithView:self.appDelegate.window];
.
[self.appDelegate.window addSubview:_progressHUD];

Works like a charm for all scenarios involving the UITableViewController. I hope this helps someone else. Happy Programming :)

like image 3
Kennedy Mulenga Avatar answered Oct 27 '22 06:10

Kennedy Mulenga


Create a category on UITableView that will take your MBProgressHUD and bring it to the front, by doing so it will always appear "on top" and let the user use other controls in your app like a back button if the action is taking to long (for example)

#import "UITableView+MBProgressView.h"

@implementation UITableView (MBProgressView)


- (void)didAddSubview:(UIView *)subview{
    for (UIView *view in self.subviews){
        if([view isKindOfClass:[MBProgressHUD class]]){
            [self bringSubviewToFront:view];
            break;
        }
    }
}

@end
like image 2
Mathieu Juneau Avatar answered Oct 27 '22 06:10

Mathieu Juneau