使用函数调用为 UIView 设置宽度约束不再起作用 iOS ( Xcode 12.0.1 )

问题描述

我在 Swift 中有以下代码来填充其容器内的状态栏,与通过动态更改其宽度来完成测验百分比有关,并且在 2018 年运行良好:

func updateUI() {
    questionCounter.text = "\(Texts.questionCounter) \(questionNumber + 1)"
    progressBar.frame.size.width = (containerOfBar.frame.size.width / CGFloat(allQuestions.list.count)) * CGFloat(questionNumber)
}

元素的实例化是由闭包以这种方式进行的:

private let containerOfBar: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.layer.cornerRadius = 8
    view.layer.borderColor = UIColor.white.cgColor
    view.layer.borderWidth = 2
    
    return view
}()

private let progressBar: UIView = {
    let bar = UIView()
    bar.backgroundColor = .blue
    bar.translatesAutoresizingMaskIntoConstraints = false
    
    return bar
}()

容器和栏的自动布局图形约束,仅在以下代码中设置,没有故事板。

酒吧本身:

progressBar.leadingAnchor.constraint(equalTo: containerOfBar.leadingAnchor,constant: 2),progressBar.topAnchor.constraint(equalTo: containerOfBar.topAnchor,progressBar.bottomAnchor.constraint(equalTo: containerOfBar.bottomAnchor,

酒吧的容器:

containerOfBar.centerXAnchor.constraint(equalTo: optionsViewContainer.centerXAnchor),containerOfBar.topAnchor.constraint(equalTo: optionsView[enter image description here][1].bottomAnchor,constant: self.view.frame.size.height/42),containerOfBar.bottomAnchor.constraint(equalTo: optionsViewContainer.bottomAnchor,constant: -self.view.frame.size.height/42),containerOfBar.widthAnchor.constraint(equalTo: optionsViewContainer.widthAnchor,multiplier: 0.3),

链接中有代码绘制的完成栏的图片。 不明白为什么 frame.width 属性不再起作用,也许是我缺少的约束工作流逻辑的变化...... 我也尝试单独使用该函数的代码,但似乎 frame.width 不再动态可用。

有什么建议吗?

Progress bar

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)