Update height constraint programmatically

Instead of adding a new constraint, you need to modify the constant on your existing constraint.

Use an IBOutlet to connect to your constraint in Interface Builder:

@property (nonatomic, weak) NSLayoutConstraint *heightConstraint;

Then, when you need to set it programmatically, simply set the constant property on the constraint:

heightConstraint.constant = 100;

OR

If you can't access the nib in Interface Builder, find the constraint in code:

NSLayoutConstraint *heightConstraint;
for (NSLayoutConstraint *constraint in myView.constraints) {
    if (constraint.firstAttribute == NSLayoutAttributeHeight) {
        heightConstraint = constraint;
        break;
    }
}
heightConstraint.constant = 100;

And in Swift:

if let constraint = (myView.constraints.filter{$0.firstAttribute == .width}.first) {
            constraint.constant = 100.0
        }

Swift 4.2

Using auto layout and animation to increase height of button

I don't use story board. all things in code.

import UIKit

class AnimateHeightConstraintViewController: UIViewController {
    var flowHeightConstraint: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        view.addSubview(button)
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
        flowHeightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
        flowHeightConstraint?.isActive = true
    }

    @objc func animateButtonTapped() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
            self.flowHeightConstraint?.constant = 100
            self.view.layoutIfNeeded()
        }, completion: nil)
    }

    lazy var button: UIButton = {
       let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = .green
        button.addTarget(self, action: #selector(animateButtonTapped), for: .touchUpInside)
        return button
    }()
}

and result is like this:

enter image description here


To get a reference of your height constraints : Click + Ctrl in the constraint and drag and drop in your class file :

enter image description here

To update constraint value :

self.heightConstraint.constant = 300;
[self.view updateConstraints];

A more flexible way using this swift extension:

extension UIView {    

    func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
        if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
            constraint.constant = constant
            self.layoutIfNeeded()
        }
    }
}

How to use this view extension to update constant value of existing constraints:

// to update height constant
testView.updateConstraint(attribute: NSLayoutAttribute.height, constant: 20.0)


// to update width constant
testView.updateConstraint(attribute: NSLayoutAttribute.width, constant: 20.0)