简体   繁体   中英

Disable dark mode for mac OSX App in Swift

Is it possible to disable the dark mode for the mac app?

I only want it to show the default style of my app, even if the user uses dark mode on his mac.

I am using a popover.

Tried to use this: self.popover.appearance = NSAppearanceNameAqua but it tells me: cannot assign value of type String to type NSApperance.

You'll need initialize the name first:

popover.appearance = NSAppearance(named: NSAppearanceNameAqua)

You were close.

Edit for Swift 4.1:

popover?.appearance = NSAppearance(named: NSAppearance.Name.aqua)

You can disable support for dark mode in the Info.plist of your app:

Opt Out of Dark Mode

Apps linked against macOS 10.14 or later should support both light and dark appearances. If you build your app against an earlier SDK, you can still enable Dark Mode support by including the NSRequiresAquaSystemAppearance key (with a value of false ) in your app's Info.plis file. Do so only if your app's appearance looks correct when running in macOS 10.14 and later with Dark Mode enabled.

If you need extra time to work on your app's Dark Mode support, you can temporarily opt out by including the NSRequiresAquaSystemAppearance key (with a value of true ) in your app's Info.plist file. Setting this key to true causes the system to ignore the user's preference and always apply a light appearance to your app.

Source: https://developer.apple.com/documentation/appkit/nsappearancecustomization/choosing_a_specific_appearance_for_your_app

If you want to force complete dark mode everywhere in your app, you can force it by simply adding NSApp.appearance = NSAppearance(named: .darkAqua) to the AppDelegate method applicationDidFinishLaunching()

Code:

func applicationDidFinishLaunching() {
    // Insert code here to initialize your application
    NSApp.appearance = NSAppearance(named: .darkAqua)
}

This works for Swift 5.5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM