Swift:右键单击后NSStatusItem保持突出显示

时间:2016-10-15 18:09:19

标签: swift action appdelegate nsmenu nsstatusitem

我在右键点击时创建了NSStatusBarItem和popUp a(以编程方式生成)NSMenu

let statusBarItem = NSStatusBar.system().statusItem(withLength: -1)
statusBarItem.action = #selector(AppDelegate.statusBarItemAction(sender:))

let menu = NSMenu()
var menuItem = NSMenuItem()
menuItem.action = #selector(AppDelegate.customItemAction)
menu.addItem(menuItem)

func statusBarItemAction(sender: NSStatusItem) {
    let mouseEvent = NSEvent.pressedMouseButtons()
    if mouseEvent == 2 {
        // right click
        lxStatusBarItem.popUpMenu(menu)
    }
}

func customItemAction() {
    // do something
}

一切正常,但statusBarItem在调用customItemAction后仍然突出显示:

enter image description here

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我发现设置statusItem.button?.isHighlighted = false有助于删除突出显示。在您的情况下,这将是这样的:

func customItemAction() {
    // do something
    statusBarItem.button?.isHighlighted = false
    ...
}