【发布时间】:2020-05-19 16:28:16
【问题描述】:
如何从另一个 ViewController 更改 MacOS 应用程序的菜单栏图标?
- AppDelegate.swift(初始化菜单栏图标)
- ViewController.swift(尝试设置菜单栏图标❌)
我找到了这个,但这并没有改变我的菜单栏图标: Mac: How to save alternate app icon in dock OSX xcode
let image = NSImage.init(named: NSImage.Name(rawValue: "AltAppIcon"))
NSApp.applicationIconImage = image
看到 BOINC 图标在其菜单栏的右下角有一个小的自定义暂停符号/徽章吗?此应用程序的图标发生变化。他们是否会改写该文件的名称并将其更改为“暂停的图标”图像?
✅更新*
设置菜单栏图标的AppDelegate.swift 函数起作用了:
AppDelegate.swift
func setIcon() {
let onIcon = NSImage(named: "fv-mini-icon-green")
statusItem.button?.image = onIcon
}
ViewController.swift
func taskOnIcon() {
DispatchQueue.main.async(execute: {
let appDele = NSApplication.shared.delegate as! AppDelegate
appDele.setIcon()
})
}
【问题讨论】:
-
菜单栏图标是
NSStatusItem实例的一部分
标签: swift xcode appdelegate menubar nsstatusitem