【问题标题】:How to fix the position of MBProgressHUD to center in a UITableView如何修复 MBProgressHUD 的位置以在 UITableView 中居中
【发布时间】:2015-02-20 06:11:48
【问题描述】:

我有一个场景,我在 UITableViewController 上使用第三方库 MBProgressHUD,但问题是每当我向上和向下滚动时,HUD 都会随着滚动而移动。

我不想禁用 UITableView 滚动,而且我想将 HUD 保持在 UITableView 的中心。

有没有可能的实现方式?

【问题讨论】:

  • 请发给我用于在 tableview 中添加 mbprogressHud 的代码...
  • 如果您遇到此类特定问题,请提供一些代码。它足以显示代码如何将MBProgressHud 添加到视图中,因此我们知道您在做什么,我们可以为您提供帮助;)

标签: ios objective-c uitableview mbprogresshud uirefreshcontrol


【解决方案1】:

将 HUD 添加到 tableview 的超级视图或 navigationController.view 可以解决问题:

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】:

    问题可能是您在 tablview 中添加 HUD,因此请在您的视图中添加...

    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"Loading";
    }
    

    【讨论】:

    • 是的,我正在将 HUD 添加到 TableViewController,但我无法更改类的实现。有没有办法让 HUD 保持在 tableview 的中心,即使我上下滚动
    【解决方案3】:

    创建 MBProgressHUD 的对象(例如:MBProgressHUD *HUD)。初始化它并在主视图上添加 HUD 作为子视图并执行此操作

    [HUD setCenter:self.view.center];
    [HUD setUserInteractionEnabled:YES];
    

    【讨论】:

    • @Jasmeent:我的主视图是从 ScrollView 派生的 TableView,每当我将 HUD 添加到表视图时,它都会随着表行向上和向下滚动。我想将它始终保持在表格视图的中心
    【解决方案4】:

    是的,有一种可能的解决方法。在[HUD show:YES]之前,添加这段代码。

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

    就是这样。

    看起来HUDview.frame 设置为超级视图的frame,因此如果您向下滚动然后显示HUD,则它位于错误的位置。添加该行会将HUDyOffset添加到向下滚动的数量中。

    【讨论】:

    • 这会将HUD的初始位置设置为滚动后的屏幕中心。但是,如果您在屏幕出现后滚动屏幕,它不会停留在屏幕中间。
    • 是的,我将它与HUD.userInteractionEnabled = NO; 一起使用。否则,您应该通过实现-(void) scrollViewDidScroll:(UIScrollView *)scrollView 在滚动视图时重置其位置
    • 知道了,谢谢!顺便说一句,现在我正在使用另一种方法,即将 HUD 附加到 UITableView 的超级视图,这样 HUD 将出现在中间而不设置 yOffset 并且默认情况下禁用用户交互。
    【解决方案5】:

    这对我有用。将其设置为 parentViewController,然后是 View。

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

    【讨论】:

      【解决方案6】:

      我尝试按照上面的说明将其添加到导航控制器中,但在将其翻译为 swift 时出现了一些解包错误。我找到了this fix,它每次都有效。感谢 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】:

          解决方案很简单。使用 tableView 时,只需将 MBProgressHUD 附加到 UIApplication.shared.delegate?.window

          对于 swift 3.0:

          显示 MBProgressHUD

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

          隐藏 MBProgressHUD

          MBProgressHUD.hide(for: ((UIApplication.shared.delegate?.window)!)!, animated: true)
          

          【讨论】:

            猜你喜欢
            • 2014-10-23
            • 2012-12-03
            • 2023-04-05
            • 2013-04-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多