问题
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)
])
how can I change its height and update heightAnchor
? I've tried this:
autoCompleteViewController
.view
.heightAnchor
.constraint(equalToConstant: initialHeightAutoCompleteViewController + CGFloat(numberOfSuggestions * 45))
.isActive = true
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.
回答1:
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)
}
Notes:
- 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.
回答2:
First you have disable the previous constraint and activate another
For example
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
then whichever constraint you need activate that and disable the other.
回答3:
If you don't want to keep a reference to the height anchor, here is an alternative...
myVC.view.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height
{
constraint.constant = newHeight
}
}
Good luck!
回答4:
You can do something like this:
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()
})
}
}
回答5:
I've fixed it by implementing it like this:
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
and then where it should be changed I added:
let newHeight = calculateNewHeight()
heightConstraintAutoCompleteViewController.constant = newHeight
It works like a charm.
来源:https://stackoverflow.com/questions/45144640/how-to-change-height-of-uiview-that-already-has-height-anchor