gpt4 book ai didi

swift - 你如何显示 NSStatusBar 项目并隐藏停靠栏图标?

转载 作者:搜寻专家 更新时间:2023-10-31 23:09:18 25 4
gpt4 key购买 nike

我试过将“Application is Agent”设置为 1,但状态栏项目消失了。当设置为 0 时,状态栏项目和停靠图标都会显示。如何显示状态栏项目但隐藏停靠栏图标?

我在 AppDelegate.swiftawakeFromNib()applicationDidFinishLaunching() 中尝试了以下操作:

//class scope    
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)

//function scope
self.statusItem.image = NSImage(named: "myImage")
let menu = NSMenu(title: "MyApp-Menu")
let menuItem = NSMenuItem(title: "title", action: nil, keyEquivalent: "")
menu.addItem(menuItem)
self.statusItem.menu = menu

最佳答案

针对 Mac OS 10.14 更新

在 AppDelegate 中:

let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)

func applicationDidFinishLaunching(_ aNotification: Notification) {
if let button = statusItem.button {
button.image = NSImage(named:NSImage.Name("StatusBarButtonImage"))
button.action = #selector(launchFromTray)
}
constructMenu()
}

func constructMenu() {
let menu = NSMenu()
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Show", action: #selector(launchFromTray), keyEquivalent: "w"))
menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q"))

statusItem.menu = menu
}

现在您应该将 Application is agent (UIElement) 设置为 YES。应该工作正常,希望这会对某人有所帮助

关于swift - 你如何显示 NSStatusBar 项目并隐藏停靠栏图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45291536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com