How to Update the Constant Height Constraint of a Uiview Programmatically

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

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

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.

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;


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;
heightConstraint.constant = 100;

And in Swift:

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

Swift UIView Constraint Constant Height Changing Time Getting LayoutConstraints Warning

Below working code for adjusting constraints constant height

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

extension UIView {

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

Layout is Broken when I update the Table View Height Constraint programmatically

You can try one thing -> change tableview height constraint priority to 100 and try to remove all fixed height of cell.
Hope it'll help you.

Try this one.It's work perfectly for me.

let cellHeight = 100.0
var cell : tableCell?
let detailArr : [UIColor] = [.red,.yellow,.black,.blue]

override func viewWillAppear(_ animated: Bool) {

func changeTableHeight()
let animator = UIViewPropertyAnimator(duration: 0.5, timingParameters: UICubicTimingParameters(animationCurve: .linear))
animator.addAnimations {
tableviewHeight.constant = CGFloat(Double(detailArr.count) * self.cellHeight)



Cell image with constraint

Table view with fix height

Table with orange background color and height as per total number of cell

UIView does not update new height constraint after action

You need to remove old 1

extension UIView {
func constrainHeight(constant: CGFloat) {
constraints.forEach {
if $0.firstAttribute == .height {

heightAnchor.constraint(equalToConstant: constant).isActive = true

Update layout constraints programmatically without IBOutlets - Swift

Store the constraint in a variable and change the constant and call layoutIfNeeded when you need it animated.

// Declare this along with the other variables in your class
var constraintVariable: NSLayoutConstraint!


// Where you set your constraints. Store the constraint to be animated in the variable and make it active
// Your other constraints
constraintVariable = sideMenu.leftAnchor.constraint(equalTo: view.leftAnchor, constant: someNegativeValue);
constraintVariable.isActive = true


@objc func slideMenu() {
UIView.animate(withDuration: suitableDuration) {
constraintVariable.constant = requiredValue

Related Topics

Leave a reply