How to show activity indicator while tableView loads?

前端 未结 12 1540
说谎
说谎 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:01

    In order to place the UIActivityIndicator in foreground, even over the eventual UITableViewController separators, I have adopted this solution.

    • I have add the UIActivityIndicator programmatically, and add it as a subview of my UINavigationController

      var activityIndicator: UIActivityIndicatorView!
      
      override func viewDidLoad() {
         super.viewDidLoad()
         // Code
         // .... omissis
      
         // Set activity indicator
         activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
         activityIndicator.color = UIColor.darkGray
         activityIndicator.center = tableView.center
         activityIndicator.hidesWhenStopped = true
         activityIndicator.stopAnimating()
         self.navigationController?.view.addSubview(activityIndicator)
      }
      
    • I have just start & stop it when needed (in my case):

      func animateActivityIndicator(_ sender: Any ) {
         guard let vc = sender as? UIViewController else { return }
         if let v = vc as? MyTableViewController {
             if v.activityIndicator.isAnimating {
                 v.activityIndicator.stopAnimating()
             } else {
                 v.activityIndicator.startAnimating()
             }
         }
         // Others UIViewController or UITableViewController follows...
         // all of them exhibits an activityIndicator variable
         // implemented programmatically or with the storyboard
      }
      

    PS. My environment is Xcode 10.0, iOS 12.0

提交回复
热议问题