0

For some reason, UIProgressView.setProgress(1, animate: true) causes a messed up animation to occur. The picture below shows the problem. First off, it animates from the center outward and it starts slightly above its actual location. enter image description here

So here is the full view controller code that contains the UIProgressView

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var progressBar: UIProgressView!

    override func viewDidLoad() {
        super.viewDidLoad()
        progressBar.setProgress(1, animated: true)
    }
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Alex Bent
  • 127
  • 1
  • 13
  • I can't understand what is wrong – Federico Malagoni Dec 20 '16 at 22:44
  • 1
    What if you wait until the view is fully loaded ? By calling `progressBar.setProgress(1, animated: true)` in `viewDidAppear` – Randy Dec 20 '16 at 22:44
  • 1
    The frame won't be set correctly in `viewDidLoad`; wait until `viewDidAppear` or `viewDidLayoutSubviews`. You may also be able to address the issue if you have a fixed width for your progress view. – Paulw11 Dec 20 '16 at 22:47
  • Thank you Randy and Paulw11 that was the problem indeed. It needed to be called not in viewDidLoad but in viewDidAppear – Alex Bent Dec 21 '16 at 00:01
  • Can confirm. Not related to either viewDidAppear OR viewDidLoad – William Yang Apr 06 '18 at 01:53

2 Answers2

1

The problem was the setProgress was called in viewDidLoad which messed it up. It needed to be called in another method that is called after the view is fully loaded such as viewDidAppear as suggested by Paulw11 and Randy

Alex Bent
  • 127
  • 1
  • 13
1

The problem here, it's you're setting up your progress view before your view loading done. You can force "building" progress view before setting up you progress using layoutIfNeeded() method.

override func viewDidLoad() {
    super.viewDidLoad()
    progressBar.layoutIfNeeded()
    progressBar.setProgress(1, animated: true)
}
Bruno Ramos
  • 149
  • 1
  • 4