在 Swift 中禁用 mac OSX 应用程序的深色模式

Disable dark mode for mac OSX App in Swift

是否可以为 mac 应用禁用深色模式?

我只希望它显示我的应用程序的默认样式,即使用户在其 mac 上使用暗模式也是如此。

我正在使用弹窗。

尝试使用这个:self.popover.appearance = NSAppearanceNameAqua 但它告诉我:无法将 String 类型的值分配给 NSApperance 类型。

您需要先初始化名称:

popover.appearance = NSAppearance(named: NSAppearanceNameAqua)

你很接近。

为 Swift 4.1 编辑:

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

您可以在应用的 Info.plist 中禁用对深色模式的支持:

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.

来源:https://developer.apple.com/documentation/appkit/nsappearancecustomization/choosing_a_specific_appearance_for_your_app

如果你想在你的应用程序的任何地方强制完全黑暗模式,你可以通过简单地添加 NSApp.appearance = NSAppearance(named: .darkAqua) 到 AppDelegate 方法来强制它 applicationDidFinishLaunching()

代码:

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

这适用于 Swift 5.5.