iosswiftuiactivityindicatorview

Why does UIActivityIndicatorView only animate the first time it is visible?


I change the navigation bar title depending on the network status, when there is no connection I display as UIActivityIndicatorView, along side "Waiting for network". This works perfectly the very first time I turn off network connectivity, when I disable it for the second time, "Waiting for network" and the Activity indicator appear, however, it doesn't animate.

 override func viewDidLoad() {
        label.text = "Waiting for network"
        label.font = UIFont.systemFont(ofSize: 17)
        label.textColor = .black
        
     
        containerView.addSubview(self.activityIndicator)
        containerView.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        self.activityIndicator.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            self.activityIndicator.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
            self.activityIndicator.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            label.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
            label.leadingAnchor.constraint(equalTo: self.activityIndicator.trailingAnchor, constant: 8),
            containerView.widthAnchor.constraint(equalTo: label.widthAnchor, constant: self.activityIndicator.frame.width + 8)
        ])
        
        super.viewDidLoad()
    }
    
    
    
    func onUpdate(_ isConnected: Bool) {
        print("isInternetConnected changed to: \(isConnected)")

        if isConnected {
            DispatchQueue.main.async {
                
                self.navigationItem.titleView = nil
                self.activityIndicator.stopAnimating()
                self.navigationItem.title = "Chats"
            }
        } else {
            DispatchQueue.main.async {
                self.navigationItem.titleView = 

self.containerView
 self.activityIndicator.startAnimating()


            }
        }
    } 

  let activityIndicator: UIActivityIndicatorView = {
        let activityIndicator = UIActivityIndicatorView()
        activityIndicator.style = .medium
        activityIndicator.hidesWhenStopped = true
        activityIndicator.color = .black
        activityIndicator.startAnimating()
        return activityIndicator
    }()
    

Solution

  • You need to start animation by calling self.activityIndicator.startAnimating(), as you stop it when isConnected is true

       if isConnected {
            DispatchQueue.main.async {
                
                self.navigationItem.titleView = nil
                if self.activityIndicator.isAnimating {
                     self.activityIndicator.stopAnimating()
                }
                self.navigationItem.title = "Chats"
            }
        } else {
            DispatchQueue.main.async {
                self.navigationItem.titleView = self.containerView
                if !self.activityIndicator.isAnimating {
                     self.activityIndicator.startAnimating() // <<< --- here
                }
            }
        }