如何在点击按钮后在NSMenu
旁边显示NSButton
。
这是我到目前为止所做的。
@IBOutlet weak var BtnMenu: NSButton!
// Menu
let appMenu = NSMenu()
let showPrefsMenuItem = NSMenuItem(title: "Preferences...", action: #selector(VC.showPrefs(_:)), keyEquivalent: "")
let showAboutMenuItem = NSMenuItem(title: "About", action: #selector(VC.showAbout(_:)), keyEquivalent: "")
let quitAppMenuItem = NSMenuItem(title: "Quit", action: #selector(VC.quitApp(_:)), keyEquivalent: "")
override func viewDidLoad() {
super.viewDidLoad()
// Set up menu
self.appMenu.addItem(self.showPrefsMenuItem)
self.appMenu.addItem(self.showAboutMenuItem)
self.appMenu.addItem(NSMenuItem.separatorItem())
self.appMenu.addItem(self.quitAppMenuItem)
}
// Shows the app menu
@IBAction func openMenu(sender: NSButton) {
self.appMenu.popUpMenuPositioningItem(self.appMenu.itemAtIndex(0), atLocation: NSEvent.mouseLocation(), inView: nil)
}
但是现在,菜单显示在鼠标位置。 Theresa是一种显示按钮旁边菜单的方法吗?
感谢。
答案 0 :(得分:8)
这就是我解决它的方法:
let p = NSPoint(x: sender.frame.origin.x, y: sender.frame.origin.y - (sender.frame.height / 2))
self.appMenu.popUp(positioning: nil, at: p, in: sender.superview)
在这种情况下,菜单显示在单击的按钮下。
答案 1 :(得分:5)
在按钮下显示NSMenu:
let p = NSPoint(x: 0, y: sender.frame.height)
appMenu.popUp(positioning: nil, at: p, in: sender)
按下按钮显示NSMenu:
let p = NSPoint(x: sender.frame.width, y: 0)
appMenu.popUp(positioning: nil, at: p, in: sender)
答案 2 :(得分:0)
试试这个:
let p = NSPoint(x: sender.frame.origin.x, y: sender.frame.origin.y)
self.appMenu.popUpMenuPositioningItem(self.appMenu.itemAtIndex(0), atLocation:p , inView: nil)
答案 3 :(得分:0)
只有丢失才能传递发件人按钮。
来自john elemans的回复:
let p = NSPoint(x: sender.frame.origin.x, y: sender.frame.origin.y)
self.appMenu.popUpMenuPositioningItem(self.appMenu.itemAtIndex(0), atLocation:p , inView: sender)
答案 4 :(得分:0)
签出This tutorial,其中包含一些有关快速弹出菜单的信息