最初我将容器的高度设置为 75。但是viewWillLoad我想更新这个值并设置为width / 5。
为此,我尝试:
containerHeight = NSLayoutConstraint(item: container, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: width / 5)
container.addConstraint(containerHeight)
Run Code Online (Sandbox Code Playgroud)
还:
containerHeight.constant = width / 5
Run Code Online (Sandbox Code Playgroud)
但两种变体都不会更新我的约束。它仍然返回给我 75. 怎么了?
如果您已经在故事板中创建了一个现有约束,您可以添加一个IBOutlet并更改其常量,不要创建一个新的...
然后把你的代码放进去 viewDidLayoutSubviews()
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
constraint.constant = 10 // Just to test and see it really works 75 -> 10
self.view.layoutIfNeeded() // If iOS 7
// I think constraints are updated after this function and you wont see a change in the frame.height until it has ran. Put your print in an async and you'll see the updated frame.
dispatch_async(dispatch_get_main_queue()) {
print(view.frame.height)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4476 次 |
| 最近记录: |