繁体   English   中英

从 ViewController 设置菜单栏图标

[英]Set menubar icon from ViewController

如何从另一个 ViewController 更改 MacOS 应用程序的菜单栏图标?

  • AppDelegate.swift(初始化菜单栏图标)
  • ViewController.swift(尝试设置菜单栏图标❌)

我发现了这个,但这并没有改变我的菜单栏图标: Mac:如何在 Dock OSX xcode 中保存备用应用程序图标

let image = NSImage.init(named: NSImage.Name(rawValue: "AltAppIcon"))
NSApp.applicationIconImage = image

看看 BOINC 图标如何在其菜单栏的右下角有一个小的自定义暂停符号/徽章? 此应用程序的图标发生变化。 他们是否会改写该文件的名称并将其更改为“暂停的图标”图像?

BOINC 菜单栏图标暂停


更新*

设置菜单栏图标的AppDelegate.swift function 工作:

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()
  })
}

这里有一个方法...

class AppDelegate: NSObject, NSApplicationDelegate {
    var statusBarItem: NSStatusItem!

    func applicationDidFinishLaunching(_ aNotification: Notification) {

        let statusBar = NSStatusBar.system
        statusBarItem = statusBar.statusItem(withLength: 16)

        let button = statusBarItem.button
        button?.image = NSImage(named: "fv-mini-icon-green")

    // .. other code here



暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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