Sahil Dhiman
Sahil Dhiman

Reputation: 311

Activity indicator is shown only once on the the same view after presenting the view

I'm setting up the ActivityInticatorView to my view. First time when I reaches the view by show segue and tap the button, everything works fine and activity indicator starts animating and stops once the process is done.

But again if I tap the same button for another process, I'm not able to see the activity indicator at all, and process finishes without showing the indicator.

Below is the code which I'm using:-

func setUpLoader(){
let loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
loadingView.backgroundColor = #colorLiteral(red: 0.3176470697, green: 0.07450980693, blue: 0.02745098062, alpha: 1)
loadingView.color = ACColorHelper.getDarkGrey()
loadingView.hidesWhenStopped = false
loadingView.startAnimating()
loadingView.center = view.center
self.view.addSubview(loadingView)
}

Here id the method where I'm setting up the indicator.

@objc func onFavPressed(sender : UIButton) {
    isDatabaseValuesChanged = true

    setUpLoader()

    if self.offlineTrailIds.contains(Int64(modelHikingTrail!.trailId)){
        deleteTrailDataFromDB() // For this indicator is not shown
         print("turn off pink")
    }else{
        saveTrailDataToDB() // For this indicator is shown and working fine.
        print("turn pink")
    }
}

The code below is where I'm hiding the indicator.

func saveTrailDataToDB() {

   self.loadingView.stopAnimating()
   self.loadingView.removeFromSuperview()
   self.collectionView.reloadData()

}

func deleteTrailDataFromDB () {
   self.loadingView.stopAnimating()
   self.loadingView.removeFromSuperview()
}

Upvotes: 0

Views: 604

Answers (4)

kannan
kannan

Reputation: 364

After checking your code i found that loadingView is declared locally in setUpLoader function try by giving it a global scope and then call removefromsuperview. Code below,

var loadingView: UIActivityIndicatorView?

func setUpLoader(){
**loadingView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)**
loadingView.backgroundColor = #colorLiteral(red: 0.3176470697, green: 0.07450980693, blue: 0.02745098062, alpha: 1)
loadingView.color = ACColorHelper.getDarkGrey()
loadingView.hidesWhenStopped = false
loadingView.startAnimating()
loadingView.center = view.center
self.view.addSubview(loadingView)
}

Upvotes: 0

Antoine El Murr
Antoine El Murr

Reputation: 317

In setUpLoader() you are creating a new variable let loadingView, so on each fab click you will create a new activityIndicator and add it to your view instead of doing that try this:

In your class create a variable:

var loadingView : UIActivityIndicatorView?

and create a function:

    func setUpLoader() {
      if loadingView == nil {
       self.loadingView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
       self.loadingView?.backgroundColor = #colorLiteral(red: 0.3176470697, green: 0.07450980693, blue: 0.02745098062, alpha: 1)
       self.loadingView?.color = ACColorHelper.getDarkGrey()
       self.loadingView?.hidesWhenStopped = true
       self.loadingView?.center = view.center
       self.view.addSubview(loadingView)
      }
      self.loadingView?.startAnimating()
    }

Instead of putting hidesWhenStopped = false change it to true so when you call self.loadingView.stopAnimating(), it will be hidden automatically

So your other functions will be in this form:

func saveTrailDataToDB() {
   self.loadingView?.stopAnimating()
   self.collectionView.reloadData()
}

func deleteTrailDataFromDB () {
   self.loadingView?.stopAnimating()
}

Upvotes: 0

Muzahid
Muzahid

Reputation: 5186

First declare loadingView variable as property of ViewController like this:

let loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)

Then call setUpLoader from viewDidLoad method.

func setUpLoader() {
    loadingView.backgroundColor = #colorLiteral(red: 0.3176470697, green: 0.07450980693, blue: 0.02745098062, alpha: 1)
    loadingView.color = ACColorHelper.getDarkGrey()
    loadingView.hidesWhenStopped = false
    loadingView.center = view.center
    self.view.addSubview(loadingView)
}

Replace the rest of the method like this:

@objc func onFavPressed(sender : UIButton) {
    isDatabaseValuesChanged = true
    loadingView.startAnimating()
    if self.offlineTrailIds.contains(Int64(modelHikingTrail!.trailId)){
        deleteTrailDataFromDB() // For this indicator is not shown
        print("turn off pink")
    } else {
        saveTrailDataToDB() // For this indicator is shown and working fine.
        print("turn pink")
    }
}

func saveTrailDataToDB() {
    self.loadingView.stopAnimating()
    self.collectionView.reloadData()
}

func deleteTrailDataFromDB () {
     self.loadingView.stopAnimating()
}

Upvotes: 0

Alexandr Kolesnik
Alexandr Kolesnik

Reputation: 2204

I use this UIView extension to obtain situation similar to yours, try it. Hope it will be helpful.

func showIndicator(color: UIColor) {
    let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: (frame.size.width - 27) / 2, y: (frame.size.height - 27) / 2, width: 27, height: 27))
    activityIndicator.color = color
    activityIndicator.startAnimating()
    addSubview(activityIndicator)
    isUserInteractionEnabled = false
}

func hideIndicator() {
    isUserInteractionEnabled = true
    for subview in subviews {
        if subview is UIActivityIndicatorView {
            subview.removeFromSuperview()
            break
        }
    }
}

Upvotes: 1

Related Questions