How to update constraints in Swift

Oscar de la Hera Gomez
A flower that represents Swift next to a flower that represents XCode. Beneath it sits the text that states 'Update Constraints'.

A step by step tutorial for updating constraints in Swift (iOS). Github repository included.

The following tutorial walks you through how to update constraints in Swift through the example of updating Safe Area Insets, and builds on our Open Source Swift starter project.

For those looking for the specific answer, it can be found in Step Two.

Please note this tutorial uses the TinyConstraints Swift package, which we added using the Swift Package Manager.

Step One: Create the extension file

A screenshot showing you how to create your ViewController+Update.swift file.

Select the RootViewController folder and press Command + N.

This will select the Swift, iOS file type. Press Next.

After that enter the name of your file - ViewController+Update - and press Create.

If you are curious as to why we created this extension, please read up on our recommendations for creating a structured custom UIView. The extensions apply to the UIViewController class.

Step Two: Code your extension

A screenshot showing you how to update your constraints within the extension file that you created.

The code below creates an extension for the ViewController and adds an update function that updates the constraint constants - which can be considered the offsets.

At the end it calls self.view.setNeedsLayout() and self.view.layoutIfNeeded().

These two functions are what make the constraints update.

Step Three: Call the function when the safe area insets update

A Screenshot showing you to call the update function after updating the safe area insets

Call the updateConstraints function within the previously coded viewSafeAreaInsetsDidChange. This will call the constraints to be updated after updating the Safe Area Insets.

Step Four: Verify

A screenshot showing you the before and after of this tutorial.

Run the app and rotate your phone, you should see the safe area insets update.

Any Questions

We are actively looking for feedback on how to improve this resource. Please send us a note to with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details