我有一个NSPopover连接到窗口的NSView。目前我有一个显示NSStatusMenu的NSStatusItem。当您单击该菜单中的某个选项时,我将菜单设置为nil,然后显示NSPopover。问题是我希望状态栏按钮在显示NSPopover时保持突出显示,但只有当我单击按钮打开NSPopover时它才会突出显示。我试过statusItem.button?.highlight(true)无济于事,似乎更改按钮类型也没有做任何事情。有任何想法吗?谢谢。此外,在打开NSPopover后,是否可以在弹出窗口中创建NSView或更具体地在NSView中选择文本字段?我将弹出窗口行为设置为瞬态,但只有先点击弹出框然后再弹出窗口,它才会关闭。
@IBOutlet weak var mainMenu: NSMenu!
@IBOutlet weak var popover: NSPopover!
@IBOutlet weak var popoverView: NSView!
@IBOutlet weak var textField: NSTextField!
// init new menu bar item
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
// init menu bar item icon
let icon = NSImage(named: "menuIcon")
icon?.setTemplate(true) // now compatible with "dark mode"
statusItem.image = icon
statusItem.menu = mainMenu
popover.behavior = NSPopoverBehavior.Transient
//statusItem.button?.setButtonType(NSButtonType.OnOffButton)
}
@IBAction func StatusItemClicked(sender: NSButton) {
if !(popover.shown) {
popover.showRelativeToRect(sender.bounds, ofView: statusItem.button!, preferredEdge: NSMinYEdge)
}
else {
popover.close()
}
}
@IBAction func movieRegular(sender: NSMenuItem) {
statusItem.menu = nil // get rid of statusItem menu
statusItem.action = Selector("StatusItemClicked:") // func StatusItemClicked called when button clicked
StatusItemClicked(statusItem.button!) // call it so popover immediately displays first time
}