我在情节提要中定义了一个自动布局约束,并将其设置为IBOutlet:
@IBOutlet weak var bottomPanelbottomConstraint:NSLayoutConstraint!
它在情节提要中的设置如下:
接下来,我更改viewDidLoad中的常量:
override func viewDidLoad() {
super.viewDidLoad()
bottomPanelbottomConstraint.constant = -12
}
但是我仍然可以在viewDidLayoutSubviews中找到此常量,此常量在此之后的某个时间为-3,这是Storyboard中的值。这怎么会发生?
编辑:以下解决方法有效,但并不理想:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if bottomPanelbottomConstraint.constant == -3 {
bottomPanelbottomConstraint.constant = -12
bottomPanel.layoutIfNeeded()
self.view.layoutIfNeeded()
}
}
答案 0 :(得分:0)
您应在更改常数后调用layoutIfNeeded()
。这将立即导致布局的强制更新。