updateConstraints()
Updates constraints for the view.
Declaration
func updateConstraints()Discussion
Override this method to optimize changes to your constraints.
To schedule a change, call setNeedsUpdateConstraints() on the view. The system then calls your implementation of updateConstraints() before the layout occurs. This lets you verify that all necessary constraints for your content are in place at a time when your custom view’s properties aren’t changing.
Your implementation must be as efficient as possible. Don’t deactivate all your constraints, then reactivate the ones you need. Instead, your app must have some way of tracking your constraints, and validating them during each update pass. Only change items that need to be changed. During each update pass, you must ensure that you have the appropriate constraints for the app’s current state.
Don’t call setNeedsUpdateConstraints() inside your implementation. Calling setNeedsUpdateConstraints() schedules another update pass, creating a feedback loop.
In iOS 18 and later, UIKit supports automatic trait tracking inside this method for traits from this view’s traitCollection. For more information, see Automatic trait tracking.
This method supports automatic observation tracking. For more information, see Updating views automatically with observation tracking.