如何更改已经具有高度锚点的UIView的高度? [英] How to change height of UIView that already has height anchor?
本文介绍了如何更改已经具有高度锚点的UIView的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这个用于子视图控制器:
If I have this for a child view controller:
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
?我已经尝试过了:
how can I change its height and update heightAnchor
? I've tried this:
autoCompleteViewController
.view
.heightAnchor
.constraint(equalToConstant: initialHeightAutoCompleteViewController + CGFloat(numberOfSuggestions * 45))
.isActive = true
但是没有运气.我还尝试添加 layoutIfNeeded()
和其他一些类似的方法,但是没有用.如何使用锚点更新视图高度?感谢您的回答.
but with no luck. I also tried to add layoutIfNeeded()
and some other similar methods but it didn't work. How can I update view height with anchors? Thanks for your answers.
推荐答案
除了@Mukesh答案,还只是更新约束:
Additionally to @Mukesh answer is simply updating the constraint:
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
.我总是遇到构建错误.
- You cannot fully declare this variable at the class level, as
autoCompleteViewController.view
is not yet instantiated. - You cannot set up the constraint and set
isActive = true
at the same time. I've always gotten a build error.
这篇关于如何更改已经具有高度锚点的UIView的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文