本文介绍了MacOS使用Swift 4更改菜单栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MacOS Mojave中新的黑暗"模式,我想更改应用程序的菜单栏图标.默认情况下,它是一只白色的黑熊,而不仅仅是反转颜色,我想使用打bear熊的图标.

With the new Dark mode in MacOS Mojave I would like to change the menubar icon of my app. In default, it is a white black bear and rather than just inverting the colors I would like to use an icon of a snoozing bear.

我所有的代码都在applicationDidFinishLaunchingAppDelegate中设置了菜单栏图标.所以我的问题是:有没有办法检查暗模式是否已激活,然后使用swift 4更改图标?

I have all my code that sets up the menubar icon in AppDelegate in applicationDidFinishLaunching. So my question is: Is there a way to check if the dark mode was activated and then change the icon using swift 4?

我尝试过:

var inDarkMode: Bool {
  let mode = UserDefaults.standard.string(forKey: "AppleInterfaceStyle")
  return mode == "Dark"
}

,然后是一个if语句

button.image = NSImage(named: NSImage.Name(rawValue: "StatusBarButtonImage"))

但是由于代码在applicationDidFinishLaunching中,因此只会检查一次.

but because the code is in applicationDidFinishLaunching this will only check once.

非常感谢您的帮助.

推荐答案

正如Leo在评论中指出的, Apple的文档清楚地解释了该过程:

As pointed to by Leo in the comments, Apple's docs explain the process clearly:

  1. 打开资产目录并添加新的图像资产.
  2. 在右侧菜单中,单击外观上的下拉菜单:
  1. open your asset catalog and add a new image asset.
  2. in the right side menu, click on the dropdown at Appearances:

默认情况下应为 Any ,将其更改为 Any,Dark Any,Light,Dark 以自定义所需的图标用于每种情况.

It should be Any by default, change it to Any, Dark, or Any, Light, Dark to customize the icons you want to use for each case.

这篇关于MacOS使用Swift 4更改菜单栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:57
查看更多