How to update constraints in Swift

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

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 07/19/2022 at 15:32
Last Updated on 08/27/2022 at 12:02
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.

Download Open Source projectHow to implement Safe Area Insets in SwiftHow to create a SwiftLint enabled Swift Xcode Project

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

Discover Tiny ConstraintsHow to add a Swift Package Manager package dependency

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.

Recommendations for creating a structured custom UIView in Swift

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 inquiries@delasign.com with any thoughts or feedback you may have.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe