7

I have a scenario where I am using third party library MBProgressHUD on a UITableViewController but the issues is whenever I scroll up and down the HUD moves with the scroll.

I don't want to disable the UITableView scroll and also I want to keep the HUD in the centre of the UITableView.

Is there a possible way of implementing it?

Er.Shreyansh Shah
  • 1,482
  • 1
  • 9
  • 29
Rishi Khanna
  • 409
  • 1
  • 5
  • 16
  • 1
    plz send me code you use to add mbprogressHud in tableview... – Chandan kumar Feb 20 '15 at 06:43
  • Please provide some code if you have a specific problem like this. Its enough to display the code how you add the `MBProgressHud` to your view, so we know what you are doing and we can help you ;) – Alex Cio Jun 18 '15 at 08:43

8 Answers8

7

Adding HUD to the tableview's super view or navigationController.view fixes the problem:

UIView *view = self.tableView.superview;
// UIView *view = self.navigationController.view; if you are using navigationController
if (view != nil) {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
}
Brian
  • 30,156
  • 15
  • 86
  • 87
0

The issue may that you are adding the HUD in tablview, so add in your view like..

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = @"Loading";
}
Chandan kumar
  • 1,074
  • 12
  • 31
  • yes I am adding the HUD to the TableViewController and I cannot change the implementation of the class. Is there a way of keeping the HUD in center of the tableview, even when I scroll up and down – Rishi Khanna Feb 20 '15 at 08:50
0

Create the object of MBProgressHUD(example: MBProgressHUD *HUD). Initialize it and add HUD as subview on your main view and do this

[HUD setCenter:self.view.center];
[HUD setUserInteractionEnabled:YES];
Jasmeet Singh
  • 564
  • 3
  • 9
  • @Jasmeent:my main view is TableView which is derived from the ScrollView, whenever I add HUD to table view it scrolls up and down with the table rows. I want to keep it in the center of the table view always – Rishi Khanna Feb 20 '15 at 08:53
0

Yes, there is a possible way to fix. Before [HUD show:YES], add this code.

HUD.yOffset = CGRectGetMinY(self.tableView.bounds);

and that's it.

It looks like HUD's view.frame is set as it's superview's frame, and therefore it's on a wrong position if you scroll down and then show HUD. Adding the line will add HUD's yOffset to the amount of scroll down.

Keunwoo Choi
  • 865
  • 7
  • 8
  • This will set the initial position of the HUD to be at center of the screen after scrolling. However if you scroll the screen after it appear it doesn't stick at the middle of the screen. – Bruce Jul 17 '15 at 07:33
  • Yes, I used it with `HUD.userInteractionEnabled = NO;`. Otherwise you should reset its position whenever the view is scrolled by implementing `-(void) scrollViewDidScroll:(UIScrollView *)scrollView` – Keunwoo Choi Jul 18 '15 at 14:07
  • Got it, thanks! Btw now I'm using an alternative way, which is to attach the HUD to UITableView's superview, that way the HUD will appear in the middle without setting yOffset and user interaction is disabled by default. – Bruce Jul 18 '15 at 14:15
0

This has worked for me. Set it to the parentViewController, then View.

let spinningActivity = MBProgressHUD.showHUDAddedTo(self.parentViewController?.view, animated:true)
GuiSoySauce
  • 1,763
  • 3
  • 24
  • 37
0

I tried adding it to the navigation controller as instructed above and I got some unwrapping errors when I translated it to swift. I found this fix and it works every time. Credit to Aleksei Kaut :-).

- (void)showLoadingIndicator
{
  [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
}

- (void)hideLoadingIndicator
{
  [MBProgressHUD hideAllHUDsForView:[UIApplication sharedApplication].keyWindow animated:YES];
}
Rob Norback
  • 6,401
  • 2
  • 34
  • 38
0

Swift 3.0

 override func viewDidLoad() {
        super.viewDidLoad()

        progress = MBProgressHUD(view: view) // <<-- That's the trick
        view.addSubview(progress!)
        view.bringSubview(toFront: progress!)
        progress?.show(animated: true)
        progress?.mode = .indeterminate
        progress?.animationType = .fade
    }
Tal Zion
  • 6,308
  • 3
  • 50
  • 73
0

Solutions is simple. When you are using a tableView, just attach the MBProgressHUD to UIApplication.shared.delegate?.window

For swift 3.0 :

To show the MBProgressHUD

MBProgressHUD.showAdded(to: ((UIApplication.shared.delegate?.window)!)!, animated: true)

To hide the MBProgressHUD

MBProgressHUD.hide(for: ((UIApplication.shared.delegate?.window)!)!, animated: true)
shahil
  • 941
  • 9
  • 20