How to observe observe changes in screen size in Swift

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

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 02/11/2023 at 20:17
Last Updated on 02/11/2023 at 20:50
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.

SubscribeHow to create a SwiftLint enabled Swift XCode project

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.

How to observe device orientation changes in Swift

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
View Repository

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.
SubscribeContact UsVisit our BlogView our ServicesView our 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