如何更新进度条扩展?

How can I update progress bar extension?

我有一个扩展程序可以在导航栏中加载进度条。

extension UINavigationController {

    public override func viewDidLoad() {
        super.viewDidLoad()

        let progressView = UIProgressView(progressViewStyle: .bar)
        self.view.addSubview(progressView)
        let navBar = self.navigationBar

        self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[navBar]-0-[progressView]", options: .directionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView, "navBar" : navBar]))
        self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[progressView]|", options: .directionLeadingToTrailing, metrics: nil, views: ["progressView" : progressView]))

        progressView.translatesAutoresizingMaskIntoConstraints = false
        progressView.setProgress(0.0, animated: true)
        
    }
}

在我的 UIViewController class 中,如何使用 progressView.setProgress(0.0, animated: true) 更新进度条?我想从 @IBAction 中将值增加到 0.1。

由于您不能在扩展中使用存储的属性,因此您必须添加一种方法来识别进度视图,例如通过分配标签

...
let progressView = UIProgressView(progressViewStyle: .bar)
progressView.tag = 1024
self.view.addSubview(progressView)
...

然后你可以创建一个更新进度视图的方法

func updateProgressView(value: Float) {
    let progressView = view.subviews.first{[=11=].tag == 1024} as! UIProgressView
    progressView.setProgress(value, animated: true)
}

func increaseProgress() {
    let progressView = view.subviews.first{[=12=].tag == 1024} as! UIProgressView
    progressView.progress += 0.1
}