MacOS Change menubar Icon using swift 4

五迷三道 提交于 2019-12-07 16:27:14

问题


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.

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?

I tried:

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

and then an if statement around

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

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

Any help much appreciated.


回答1:


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

  1. open your asset catalog and add a new image asset.
  2. in the right side menu, click on the dropdown at Appearances:

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.



来源:https://stackoverflow.com/questions/52768874/macos-change-menubar-icon-using-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!