Kai Zheng
Kai Zheng

Reputation: 8130

Swift macOS | Setting (switching between) dark and lightmode

I am working on a macOS app where I want to give users the ability to switch between light and dark mode.

For iOS apps, this can be done by simply overriding the UserInterfaceStyle of UIWindow. Like so:

window.overrideUserInterfaceStyle = .dark //.light

The problem: NSWindow doesn't have a UserInterfaceStyle property.

I have tried to set the NSAppearance

window.appearance = NSAppearance(appearanceNamed: NSAppearance.Name.aqua, bundle: nil)

without success. It returns me an error saying: "RunTimeThemeRefForBundleIdentifierAndName() couldn't find NSAppearanceNameAqua.car in bundle with identifier: ..."

I am stuck. Do you have any ideas?

Here would be a solution for iOS

Thanks!

Upvotes: 1

Views: 563

Answers (1)

Kai Zheng
Kai Zheng

Reputation: 8130

I was on the right track!

window.appearance = NSAppearance(named: .aqua) 

worked.

There is actually a guide to set the appearance of macOS apps from apple.

Note: This also works if you have all your content inside a NSPopover, since it too has a property appearance.

Upvotes: 4

Related Questions