How to show activity indicator while tableView loads?

前端 未结 12 1564
说谎
说谎 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 21:58

    This code can help you :)

       let indicator:UIActivityIndicatorView = UIActivityIndicatorView  (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray) 
       indicator.color = UIColor .magentaColor()
       indicator.frame = CGRectMake(0.0, 0.0, 10.0, 10.0)
       indicator.center = self.view.center
       self.view.addSubview(indicator)
       indicator.bringSubviewToFront(self.view)
       indicator.startAnimating()
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-30 22:04

    Swift 3.0

    // UIView Extension

    fileprivate var ActivityIndicatorViewAssociativeKey = "ActivityIndicatorViewAssociativeKey"
    public extension UIView {
       var activityIndicatorView: UIActivityIndicatorView {
            get {
                if let activityIndicatorView = getAssociatedObject(&ActivityIndicatorViewAssociativeKey) as? UIActivityIndicatorView {
                    return activityIndicatorView
                } else {
                    let activityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
                    activityIndicatorView.activityIndicatorViewStyle = .gray
                    activityIndicatorView.color = .gray
                    activityIndicatorView.center = center
                    activityIndicatorView.hidesWhenStopped = true
                    addSubview(activityIndicatorView)
    
                    setAssociatedObject(activityIndicatorView, associativeKey: &ActivityIndicatorViewAssociativeKey, policy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                    return activityIndicatorView
                }
            }
    
            set {
                addSubview(newValue)
                setAssociatedObject(newValue, associativeKey:&ActivityIndicatorViewAssociativeKey, policy: .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
        }
    }
    

    // NSObject Extension

    public extension NSObject {
        func setAssociatedObject(_ value: AnyObject?, associativeKey: UnsafeRawPointer, policy: objc_AssociationPolicy) {
            if let valueAsAnyObject = value {
                objc_setAssociatedObject(self, associativeKey, valueAsAnyObject, policy)
            }
        }
    
        func getAssociatedObject(_ associativeKey: UnsafeRawPointer) -> Any? {
            guard let valueAsType = objc_getAssociatedObject(self, associativeKey) else {
                return nil
            }
            return valueAsType
        }
    }
    

    start animation

    tableView.activityIndicatorView.startAnimating()

    stop animation

    tableView.activityIndicatorView.stopAnimating()

    You can find more code in Magic

    0 讨论(0)
  • 2021-01-30 22:06

    Swift 2+

        class ViewController: UITableViewController {
            weak var activityIndicatorView: UIActivityIndicatorView!
    
            override func viewDidLoad() {
                super.viewDidLoad()
                let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
                tableView.backgroundView = activityIndicatorView
                self.activityIndicatorView = activityIndicatorView
                activityIndicatorView.startAnimating()
    
            }
            ...
        }
    
    0 讨论(0)
  • 2021-01-30 22:07

    SWIFT

    Place this below your class:

    let indicator:UIActivityIndicatorView = UIActivityIndicatorView  (activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    

    Place this in your loadView():

    indicator.color = UIColor .magentaColor()
    indicator.frame = CGRectMake(0.0, 0.0, 10.0, 10.0)
    indicator.center = self.view.center
    self.view.addSubview(indicator)
    indicator.bringSubviewToFront(self.view)
    indicator.startAnimating()
    

    In my case, I am requesting json objects through a func request, so I placed this at the end of that function to remove the activity indicator once the data loads:

    self.indicator.stopAnimating()
    self.indicator.hidesWhenStopped = true
    
    0 讨论(0)
  • 2021-01-30 22:10

    Another approach, In my code I added an extension for UITableView (Swift 2.3) :

    extension UITableView {
        func activityIndicator(center: CGPoint = CGPointZero, loadingInProgress: Bool) {
            let tag = 12093
            if loadingInProgress {
                var indicator = UIActivityIndicatorView()
                indicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 40, 40))
                indicator.tag = tag
                indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
                indicator.color = //Your color here
                indicator.center = center
                indicator.startAnimating()
                indicator.hidesWhenStopped = true
                self.superview?.addSubview(indicator)
            }else {
                 if let indicator = self.superview?.viewWithTag(tag) as? UIActivityIndicatorView { {
                    indicator.stopAnimating()
                    indicator.removeFromSuperview()
                }
            }
        }
    }
    

    Note : My tableview is embedded in a UIView (superview)

    0 讨论(0)
提交回复
热议问题