Walking
Walking

Reputation: 477

ios swift - update constraints programmatically

What is the equivalent to pressing the update constraint button in the interface builder auto layout but in code?

I'm updating the size of some subviews and wish to call that update constraints in code.

Upvotes: 0

Views: 933

Answers (1)

tktsubota
tktsubota

Reputation: 9411

I think you are looking for:

view.setNeedsUpdateConstraints()
view.updateConstraintsIfNeeded()

However, know that the system automatically calls this every layout pass, so you could just use the first line of code unless you need the measurements of the newest constraints. From the documentation:

Whenever a new layout pass is triggered for a view, the system invokes this method to ensure that any constraints for the view and its subviews are updated with information from the current view hierarchy and its constraints. This method is called automatically by the system, but may be invoked manually if you need to examine the most up to date constraints.

Upvotes: 1

Related Questions