How to show activity indicator while tableView loads?

前端 未结 12 1545
说谎
说谎 2021-01-30 21:44

When I switch between my tabs it loads some seconds and I want to know that my data is loading. For that I decided to add an activity indicator.

I wrote a little functio

12条回答
  •  深忆病人
    2021-01-30 22:21

    Just try this:

    var indicator = UIActivityIndicatorView()
    
    func activityIndicator() {
        indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40))
        indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        indicator.center = self.view.center
        self.view.addSubview(indicator)    
    }
    

    And where you want to start animating

    indicator.startAnimating()
    indicator.backgroundColor = UIColor.whiteColor()
    

    For stop:

    indicator.stopAnimating()
    indicator.hidesWhenStopped = true
    

    Note: Avoid the calling of start and stop at the same time. Just give some conditions.

    SWIFT : 4.2 Just try this:

    var indicator = UIActivityIndicatorView()
    
    func activityIndicator() {
        indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        indicator.style = UIActivityIndicatorView.Style.gray
        indicator.center = self.view.center
        self.view.addSubview(indicator)   
    }
    

    And where you want to start animating

    activityIndicator()
    indicator.startAnimating()
    indicator.backgroundColor = .white
    

    For stop:

    indicator.stopAnimating()
    indicator.hidesWhenStopped = true
    

提交回复
热议问题