2

I use MBProgressHUD showWhileExecuting to query some data from network,but the MBProgressHUD is not center sometimes

enter image description here

on pictures(red number):

loading displayed on left top and the backgound displayed at center.

1.loading display is not on background of MBProgressHUD 2.just show a background that doesn't contain loading

-(void)viewDidLoad
{
    //other code ...

    mb = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    // --> i try which causes also an error
    //[[MBProgressHUD alloc] initWithWindow:self.view.window]; 
    // --> error too
    //[[MBProgressHUD alloc] initWithView:self.view];
    mb.labelText = @"loding city...";

    [self.navigationController.view addSubview:mb];
    [mb showWhileExecuting:@selector(queryCity:) 
                  onTarget:self 
                withObject:[NSNumber numberWithInt:1] 
             animated:YES];
}

-(void)queryCity:(NSNumber*)flag
{
    //query some data from network.
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
zt9788
  • 948
  • 4
  • 16
  • 31

4 Answers4

2

FYI: I fixed it by saying

[[[UIApplication sharedApplication] keyWindow] addSubview:_progress];

instead of

[self.view addSubview:_progress];

I don't really like it, also because it works with self.view in another case, but I didn't see another option here.

StuFF mc
  • 4,137
  • 2
  • 33
  • 32
  • Check out my code, its simple if your using the `MBProgressHud` and you don't have to access any `UIApplication` instances by yourself ;) – Alex Cio Jun 18 '15 at 08:41
1

i find this problem. when MBProgressHUD cleanup it will lost rect.

- (void)cleanUp {
taskInProgress = NO;
    //changed by zt9788 2012-12-26
    //comment this line to fix it.
//self.indicator = nil;
#if !__has_feature(objc_arc)
[targetForExecution release];
[objectForExecution release];
#else
targetForExecution = nil;
objectForExecution = nil;
#endif
[self hide:useAnimation];

}

zt9788
  • 948
  • 4
  • 16
  • 31
1

I had the same problem. Have a look at this, solved it for me and was very simple:

[MBProgressHUD showHUDAddedTo:_selfWeak.viewOverlay animated:YES].center 
 = self.viewOverlay.center;

If you save the instancetype you receive by calling showHUDAddedTo:animated: you should be able to do some more stuff.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
0

The MBProgressHUD automatically positions itself on the view that you add it to. Since you added it to the Navigation Controller View, it positioned itself to block any interaction with the Navigation Bar while in progress.

If that is not what you want to happen, then add the MBProgressHUD subview to a different view, in this case you probably want it on self.view rather than self.navigationcontroller.view.

David Brunow
  • 1,299
  • 1
  • 12
  • 13