Search code examples
iosswiftuiprogressview

Setting UIProgressView progress to 0


I have a UIProgressView which runs for 2.5 seconds however sometimes, like if the app is being resumed from multitasking, the loading bar will start halfway up. How would I reset the UIProgressView to 0.0 each time it is opened? I could set to 0 in the viewDidLoad and then start the timer and animate it in the viewDidAppear. I just need to know how I can set my progressView to 0.


Solution

  • You set it on the viewWillAppear, thats what it calls when the view will be displayed whether when you push it into the hierarchy ro when you resume from the background.

    override func viewWillAppear() {
        super viewWillAppear();
        self.progressView.progress = 0.f;
    }
    

    You can also use the UIApplicationDidBecomeActiveNotification.

    //inside init
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateProgressBar", name: UIDeviceBatteryLevelDidChangeNotification, object: nil);
    
    //on dealloc
    NSNotificationCenter.defaultCenter().removeObserver(self);
    
    
    //the update method
    func updateProgressBar() {
        self.progressBar.progress = 0.f;
    }