代码之家  ›  专栏  ›  技术社区  ›  user979331

Swift 4 UIActivityIndicator视图未以UITableView为中心

  •  0
  • user979331  · 技术社区  · 6 年前

    我试图将我的UIActivityIndicatorView集中在UITableView中,这就是我创建UIActivityIndicatorView的方式:

    indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) as UIActivityIndicatorView
    
    //Set the activity indictor center
    indicator.center = self.view.center
    
    //Hide the indicator when its stopped.
    indicator.hidesWhenStopped = true
    
    //Set the style of the activity indicator
    indicator.style = UIActivityIndicatorView.Style.white
    
    //Set the background colour of the activity indicator
    indicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
    
    //Make the activity indicator have rounded corners
    indicator.layer.cornerRadius = 15
    
    //Add activity indicator to view
    self.view.addSubview(indicator)
    
    //Start activity indicator
    self.indicator.startAnimating()
    

    但当我在UITableView上向上滚动时,我看不到我的UIActivityIndicator视图,我尝试了以下方法:

    override func viewWillLayoutSubviews() {
        self.indicator.center = self.view.center
    }
    

    但这并没有奏效。

    我也尝试过:

    override func viewWillLayoutSubviews() {
        self.indicator.translatesAutoresizingMaskIntoConstraints = true
        self.indicator.frame = self.view.bounds
        self.indicator.center = self.view.center
    }
    

    也没用,我做错了什么?

    当我向下滚动,然后在我的表格视图中选择一个项目时,就会出现活动指示器。

    0 回复  |  直到 6 年前
        1
  •  0
  •   D. Pratt    6 年前

    不能在UITableViewController内居中,因为它是滚动视图,而滚动视图是视图控制器的视图。但是,可以将其置于窗口视图的中心,如下所示:

    // get the visible window
    let window = UIApplication.shared.keyWindow!
    let viewActivity = UIActivityIndicatorView(style: .whiteLarge)
    // set your activity indicator's center to the center of the screen
    viewActivity.center = window.center
    viewActivity.hidesWhenStopped = true
    // add the indicator to the active window (not your uitableviewcontroller) 
    // and make sure it is at the front (so it is visible)
    window.addSubview(viewActivity)
    window.bringSubviewToFront(viewActivity)
    viewActivity.startAnimating()
    

    您还可以通过抓取根导航或选项卡视图控制器,并将其居中来实现这一点。

        2
  •  0
  •   ArchiKu    5 年前

    我遇到过这样的情况:UIViewController上的一切都很好,但UITableViewController上的UIActivityIndicatorView出现了问题。

    请注意提议的解决方案。在我的例子中,activityView扩展到了整个表单,这个解决方案帮助了我。 https://stackoverflow.com/a/41886375/7938405