Search code examples
iosswiftuiprogressview

How to reset UIProgressView to 0 when using observedProgress


[XCode 8, Swift 3] I am using a UIProgressView by setting the observedProgress property to an instance of a Progress object, i.e.:

progressView.observedProgress = myProgress

myProgress.totalUnitCount is initially 0 and the UIProgressView correctly displays 0%. The UIProgressView also tracks the myProgress value beautifully as it changes. However, after the task in question is complete, I'd like to reset the UIProgressView to 0%. I'd expect this to do the job:

    myProgress.totalUnitCount = 0
    myProgress.completedUnitCount = 0

This doesn't work however, the progress bar continues to show 100%. Even though myProgress.fractionCompleted returns 0.0! I can force the progress bar to return to 0% by doing this:

progressView.progress = 0.0

However, this requires direct access to the view and somewhat defeats the beauty of observing the Progress object.

I haven't been able to find any reset concept on a Progress object. What am I missing?


Solution

  • From @Sweeper this seems to work:

    myProgress.totalUnitCount = 10
    myProgress.completedUnitCount = 0
    myProgress.totalUnitCount = 0
    

    I didn't like Sweeper's solution on its face because it leaves the Progress object in an inaccurate state. But, setting totalUnitCount twice does seem to work, the progress bar must get both updates because it shows 0% and then is accurate for further updates. I would argue that 0/0 should show 0%, i.e. this is a bug in UIProgressView. Anyway, thanks Sweeper!