How to update the constant height constraint of a UIView programmatically?

Chris Mikkelsen picture Chris Mikkelsen · Mar 8, 2017 · Viewed 143.2k times · Source

I have a UIView and I set the constraints using Xcode Interface Builder.

Now I need to update that UIView instance's height constant programmatically.

There is a function that goes like myUIView.updateConstraints(), but I don't know how to use it.

Answer

Parth Adroja picture Parth Adroja · Mar 8, 2017

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code.

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

Method updateConstraints() is an instance method of UIView. It is helpful when you are setting the constraints programmatically. It updates constraints for the view. For more detail click here.