如何更改已经具有高度锚点的 UIView 的高度?

How can I change height of UIView that already has height anchor?

如果我有一个子视图控制器:

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() 和其他一些类似的方法,但没有用。如何使用锚点更新视图高度?

首先,您禁用了先前的约束并激活了另一个

例如

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

然后您需要激活那个约束并禁用另一个。

除@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)
}

备注:

  • 您不能在 class 级别完全声明此变量,因为 autoCompleteViewController.view 尚未实例化。
  • 您不能同时设置约束 设置 isActive = true。我总是遇到构建错误。

你可以这样做:

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{[=10=].firstAttribute == .height}.first) {

                constraint.constant = self.constantValue
            }

            self.view.layoutIfNeeded()
        })
    }

}

我已经通过像这样实现它来修复它:

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

它就像一个魅力。

如果您不想保留对高度锚点的引用,这里有一个替代方法:

myVC.view.constraints.forEach { (constraint) in
     if constraint.firstAttribute == .height
     {
         constraint.constant = newHeight
     }
 }