Show NSMenu next to NSButton in Swift macOS

This is how I solved it:

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)

In this case the menu is displayed under the clicked button.


Swift 3.

Show NSMenu under the button:

    let p = NSPoint(x: 0, y: sender.frame.height)
    appMenu.popUp(positioning: nil, at: p, in: sender)

Show NSMenu to the right of the button:

    let p = NSPoint(x: sender.frame.width, y: 0)
    appMenu.popUp(positioning: nil, at: p, in: sender)

Tags:

Macos

Cocoa

Swift