Alex Harrison
Alex Harrison

Reputation: 63

Updating the Height Constraint of a View Inside of a UIStackView

Currently, I set the height and width constraints of one of my views that I later add to the stack view as follows: (For your information, productsTable is a UITableView.)

    productsTable.heightAnchor.constraintEqualToConstant(tableHeight).active = true
    productsTable.widthAnchor.constraintEqualToConstant(stackWidth).active = true
    stackView.insertArrangedSubview(productsTable, atIndex: productsTableIndex)

Later, in ViewWillAppear, I want to change the productsTable height as follows:

productsTable.heightAnchor.constraintEqualToConstant(newHeight).active = true.

Despite this, the table view remains the same size after (changing/updating) the constraint in ViewWillAppear. Is there anything I am doing wrong or can do differently to achieve the desired effect?

Upvotes: 1

Views: 2750

Answers (1)

user4958970
user4958970

Reputation:

You need to keep a reference to the height constraint that you create the first time.

let heightConstraint = productsTable.heightAnchor.constraintEqualToConstant(tableHeight)
heightConstraint.active = true

Later, in viewWillAppear() you'll be able to directly set the constant attribute of this constraint to newHeight.

heightConstraint.constant = newHeight

Upvotes: 5

Related Questions