How to change height of UIView that already has height anchor?

前端 未结 5 406

If I have this for a child view controller:

autoCompleteViewController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.act         


        
5条回答
  •  悲&欢浪女
    2021-01-13 13:30

    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.

提交回复
热议问题