How to fix the position of MBProgressHUD to center in a UITableView

杀马特。学长 韩版系。学妹 提交于 2019-12-08 17:40:15

问题


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?


回答1:


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];
}



回答2:


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";
}



回答3:


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];



回答4:


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.




回答5:


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

let spinningActivity = MBProgressHUD.showHUDAddedTo(self.parentViewController?.view, animated:true)



回答6:


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];
}



回答7:


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
    }



回答8:


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)


来源:https://stackoverflow.com/questions/28622678/how-to-fix-the-position-of-mbprogresshud-to-center-in-a-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!