如何正确更新导航控制器下方的 UIProgressView?

How to update UIProgressView below navigation controller properly?

我的目标是导航栏下方的进度条,正如您在这张截图中所见,它工作得非常好:

这是创建它的代码:

class NavigationController: UINavigationController {

    let progressView = UIProgressView(progressViewStyle: .Bar)

    override func viewDidLoad() {
        super.viewDidLoad()

        progressView.progress = 0.9
        view.addSubview(progressView)

        let bottomConstraint = NSLayoutConstraint(item: navigationBar, attribute: .Bottom, relatedBy: .Equal, toItem: progressView, attribute: .Bottom, multiplier: 1, constant: 1)
        let leftConstraint = NSLayoutConstraint(item: navigationBar, attribute: .Leading, relatedBy: .Equal, toItem: progressView, attribute: .Leading, multiplier: 1, constant: 0)
        let rightConstraint = NSLayoutConstraint(item: navigationBar, attribute: .Trailing, relatedBy: .Equal, toItem: progressView, attribute: .Trailing, multiplier: 1, constant: 0)

        progressView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints([bottomConstraint, leftConstraint, rightConstraint])
        progressView.setProgress(0.8, animated: true)
    }
}

但是当我尝试通过按上传按钮更新进度值时,

for value in [0.0, 0.25, 0.5, 0.75, 1.0] {
    NSThread.sleepForTimeInterval(0.5)
    let navC = navigationController as! NavigationController // shorthand
    navC.progressView.setProgress(Float(value), animated: true)
    let isMainThread = NSThread.isMainThread() // yes, it is main thread
    let currentValue = navC.progressView.progress // yes, the value is updated
}

没有任何反应,但是对于最后一个值 1.0 突然进度已满。我究竟做错了什么?

您是否尝试过在另一个线程上执行此操作以便像这样完美地更新它:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           for value in [0.0, 0.25, 0.5, 0.75, 1.0] 
 {
    NSThread.sleepForTimeInterval(0.5)
    let navC = navigationController as! NavigationController // shorthand
    navC.progressView.setProgress(Float(value), animated: true)
    let isMainThread = NSThread.isMainThread() // yes, it is main thread
    let currentValue = navC.progressView.progress // yes, the value is updated
 }    
});
var queue = dispatch_queue_create("a", nil)
dispatch_async(queue, {
    for value in [0.0, 0.25, 0.5, 0.75, 1.0] {
        NSThread.sleepForTimeInterval(0.5)

        dispatch_async(dispatch_get_main_queue(), {
            let navC = navigationController as! NavigationController // shorthand
            navC.progressView.setProgress(Float(value), animated: true)
        })
    }
})