How to observe observe changes in screen size in Swift

Oscar de la Hera Gomez
Two flowers that represent Swift and XCode, beneath them sits the text "Changes in View Controller View Size."

A step by step guide on observing when the view controller's view's size changes in Swift.

The following tutorial uses our Open Source Swift project to walk you through how to detect when the view controller's view's size changes on iPhone, iPad or Mac.

This tutorial also works to register changes in orientation, however if you are looking for a tutorial that focuses specifically on a way to do that without screen size changes, please consult the tutorial below.

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/view-transitions branch.

git clone git@github.com:delasign/swift-starter-project.git

Step One: Add the viewWillTransitionToSize function

A screenshot of XCode with a highlight on the viewWillTransition function in the ViewController.swift file.

In ViewController.swift, or your chosen ViewController, add the viewWillTransitionToSize function using code similar to the one below.

Step Two: Verify

A screenshot of XCode showing the console registering changes in orientation and screensize.

Run the app in XCode on an iPhone, iPad or Mac and change the orientation or screen size and you will see the console display the frame and orientation.

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.
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