如果我有这个用于子视图控制器:
autoCompleteViewController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
autoCompleteViewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
autoCompleteViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
autoCompleteViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
autoCompleteViewController.view.bottomAnchor.constraint(equalTo: googleMapViewController.view.topAnchor, constant: 0),
autoCompleteViewController.view.heightAnchor.constraint(equalToConstant: 44.0)
])
如何更改其高度并更新heightAnchor
?我试过这个:
autoCompleteViewController
.view
.heightAnchor
.constraint(equalToConstant: initialHeightAutoCompleteViewController + CGFloat(numberOfSuggestions * 45))
.isActive = true
但没有运气。我还尝试添加layoutIfNeeded()
和其他一些类似的方法,但它没有用。如何使用锚点更新视图高度?谢谢你的回答。
答案 0 :(得分:5)
除了@Mukesh,答案只是更新约束:
var heightAnchor:NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
heightAnchor = autoCompleteViewController.view.heightAnchor.constraint(equalToConstant:44.0)
heightAnchor.isActive = true
}
func changeMyHeight(numberOfSuggestions: Int) {
heightAnchor.constant = 44.0 + CGFloat(numberOfSuggestions * 45)
}
注意:
autoCompleteViewController.view
尚未实例化。isActive = true
。我总是遇到构建错误。答案 1 :(得分:4)
首先,您已禁用上一个约束并激活另一个
例如
let heightAnchor_one = autoCompleteViewController.view.heightAnchor.constraint(equalToConstant: x)
let heightAnchor_two = autoCompleteViewController.view.heightAnchor.constraint(equalToConstant: y)
heightAnchor_one.isActive = true
heightAnchor_two.isActive = false
然后你需要激活哪个约束并禁用另一个约束。
答案 2 :(得分:1)
如果您不想保留对高度锚点的引用,则可以选择...
barChart
祝你好运!
答案 3 :(得分:0)
您可以这样做:
class ViewController: UIViewController {
var constantValue: CGFloat = 44
let childView: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(childView)
childView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
childView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
childView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
childView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
childView.heightAnchor.constraint(equalToConstant: constantValue)
])
}
@IBAction func changeHeight(_ sender: UIButton) {
constantValue = 100
UIView.animate(withDuration: 0.5, animations: {
if let constraint = (self.childView.constraints.filter{$0.firstAttribute == .height}.first) {
constraint.constant = self.constantValue
}
self.view.layoutIfNeeded()
})
}
}
答案 4 :(得分:-1)
我通过这样实现它来修复它:
autoCompleteViewControllerHeight = NSLayoutConstraint(
item: autoCompleteViewController.view,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .height,
multiplier: 1.0,
constant: 44.0)
guard let autoCompleteViewControllerHeight = autoCompleteViewControllerHeight else { return }
autoCompleteViewControllerHeight.isActive = true
然后应该改变的地方我添加了:
let newHeight = calculateNewHeight()
heightConstraintAutoCompleteViewController.constant = newHeight
它就像一个魅力。