Optimal Way of Getting Screen Resolution Using SwiftUI

Hi all, I am looking for a futureproof way of getting the Screen Resolution of my display device using SwiftUI in MacOS. I understand that it can't really be done to the fullest extent, meaning that the closest API we have is the GeometeryProxy and that would only result in the resolution of the parent view, which in the MacOS case would not give us the display's screen resolution. The only viable option I am left with is NSScreen.frame.

However, my issue here is that it seems like Apple is moving towards SwiftUI aggressively, and in order to futureproof my application I need to not rely on AppKit methods as much. Hence, my question: Is there a way to get the Screen Resolution of a Display using SwiftUI that Apple itself recommends? If not, then can I rely safely on NSScreen's frame API?

I won't recommend you use NSScreen.frame because it gives you information on the available displays and not the scene or in cases where your app supports multiple scene, multitasking or resizing.

For SwiftUI, use GeometryReader and for UIkit/AppKit use UIWindowScene.coordinateSpace.bound or UIWindow bounds or frame, to get the location and size in its coordinate space.

I understand that using GeometryReader in SwiftUI or accessing an NSWindow’s bounds/frame in AppKit is ideal for obtaining the dimensions of a specific window or view. However, my objective is different — I need to determine the resolution of the entire, usable screen available on macOS. In other words, I’m looking to retrieve the full physical display size (or the “usable” portion, accounting for elements like the menu bar or dock) rather than just the size of the active window.

Could Apple recommend a futureproof way to obtain this global screen resolution? Or, if no alternative exists, is it acceptable to continue using NSScreen.frame (or NSScreen.visibleFrame for usable space) despite its drawbacks in multi-window or multitasking scenarios?

I’ll start by prefixing that you need to think of your Apps UI in-terms of Scenes and not Screen.

  • The notion of full screen or full physical display size is a bit overloaded because apps need to support unseen size classes and dynamic resizing. They could also include cases where multiple instances(Scenes) of your UI is opened in a multi tasking environment.

That said, in Appkit, you could still get a description of the screen a window NSScreen.frame but I would strongly caution you away from using screen objects to make decisions about your app’s UI and only use a screen object only as needed to retrieve screen-related information, such as the screen’s bounds rectangle, brightness, and overscan settings.

You wrote:

Could Apple recommend a futureproof way to obtain this global screen resolution? Or, if no alternative exists, is it acceptable to continue using NSScreen.frame (or NSScreen.visibleFrame for usable space) despite its drawbacks in multi-window or multitasking scenarios?

I cannot comment about the future but as of today, SwiftUI doesn’t have a way to read information about the screen the scene is in. You can file an enhancement request with your use case why you need that information.

Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

Currently in SwiftUI, you can use a GeometryReader to read the geometry prosed to it by its parent which at the root-most view is contained within a Scene(Window).

Optimal Way of Getting Screen Resolution Using SwiftUI
 
 
Q