Detecting Screen Size/Resolution Changes in macOS with SwiftUI

Bezaleel Ashefor
1 min readDec 29, 2021

--

Viso 5. Completely redesign. Launching soon.

Working on a completely new redesigned update for Viso and I wanted a way to detect screen resolution changes or when the user changed monitor configurations (plugged a new main monitor in or returned from a multiscreen setup back to their Macbook). I then used this to resize the images that were currently opened.

The process is fairly straightforward. The event you have to listen for is the NSApplication.didChangeScreenParametersNotification event.

First, we create a variable to hold this event.

Then we listen for it with the onReceive(_:perform:) method in SwiftUI. You can add this to any SwiftUI view you want to do the listening on.

You do need to compare the new size with the old size because things like moving or changing the dock position would raise this event. To be also on the safe side, make sure all your calculations on the screen come from the visibleFrame.

Let me know if this was helpful!

--

--

Bezaleel Ashefor

Creator. I am also an iOS/macOS engineer building Viso (https://getviso.app) and Litur (https://litur.app). Sometimes I moonlight as a full stack engineer