简体   繁体   English

从 ViewController 设置菜单栏图标

[英]Set menubar icon from ViewController

How to change the menubar icon of a MacOS app from another ViewController?如何从另一个 ViewController 更改 MacOS 应用程序的菜单栏图标?

  • AppDelegate.swift (inits menubar icon) AppDelegate.swift(初始化菜单栏图标)
  • ViewController.swift (tries to set menubar icon ❌) ViewController.swift(尝试设置菜单栏图标❌)

I found this but this isn't changing the menubar icon for me: Mac: How to save alternate app icon in dock OSX xcode我发现了这个,但这并没有改变我的菜单栏图标: Mac:如何在 Dock OSX xcode 中保存备用应用程序图标

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

See how the BOINC icon has the little custom pause symbol/badge in the bottom right of it's menubar?看看 BOINC 图标如何在其菜单栏的右下角有一个小的自定义暂停符号/徽章? This app's icon changes.此应用程序的图标发生变化。 Are they writing over the name of that file and changing it to the "paused icon" image maybe?他们是否会改写该文件的名称并将其更改为“暂停的图标”图像?

BOINC 菜单栏图标暂停


UPDATE* 更新*

A AppDelegate.swift function that set the menubar icon worked:设置菜单栏图标的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()
  })
}

Here is a way...这里有一个方法...

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