How to Detect the Retina Display for macOS in SwiftUI

In Cocoa, you can find out whether or not you have a Retina screen with the backingScaleFactor property like the following.

func getWinFactor() -> CGFloat? {
	if let view = self.view.window {
		let factor = view.backingScaleFactor
		return factor
	} else {
		return nil
	}
}

How could we detect whether or not the application is dealing with a Retina screen in SwiftUI? I thought the displayScale Environment property is the chosen one. But my 27-inch iMac with a Retina display will return the scale as 1.0.

import SwiftUI

struct ContentView: View {
	@Environment(\.displayScale) var displayScale

	var body: some View {
		VStack {
			...
		}
		.onAppear {
			print("display scale: \(displayScale)") // Returning 1.0
		}
	}
}

Do I miss something with this environment guy? Muchos thankos.

Post not yet marked as solved Up vote post of Tomato Down vote post of Tomato
491 views

Replies

For whatever reason, I'm getting the expected "2.0" with my Studio Display.

(There's also pixelLength, which returns the length of one pixel in points.)

  • Thanks. I don't remember exactly when I posted this topic. My Mac mini with a non-Retina display arrived exactly one month ago on September 28th, 2023. I strongly doubt that I ran the code on my Mac mini. And I'm quite certain that the code has returned 1.0.

  • Okay. I've tested the code with my Retina 5K (5,120 x 2,880) iMac (6-Core Intel Core i5, 3 GHz, Built-In Retina LCD). It has returned 1.0.

  • I get the same result under Xcode 14 and Xcode 15.