'How to open a NSPopover at a distance from the system bar?
I'm opening a NSPopover with the action of an icon in the status bar.
myPopover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
This works fine with the exception that the distance from the popover and the system bar is zero:
I'd like to achieve the same result as the Dropbox app which renders the popover at a small distance from the system bar:
I've tried using button.bounds.offsetBy(dx: 0.0, dy: 20.0) which doesn't affect the position of the popover and button.bounds.offsetBy(dx: 0.0, dy: -20.0) which puts the popover above the system bar:
So how can I position the NSPopover at some distance from the system bar?
Solution 1:[1]
you can move the contentView of the popover right after showing it:
myPopover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
let popoverWindowX = myPopover.contentViewController?.view.window?.frame.origin.x ?? 0
let popoverWindowY = myPopover.contentViewController?.view.window?.frame.origin.y ?? 0
myPopover.contentViewController?.view.window?.setFrameOrigin(
NSPoint(x: popoverWindowX, y: popoverWindowY + 20)
)
myPopover.contentViewController?.view.window?.makeKey()
in terms of UI you will get a slight slide of the arrow but in your case, with the very small padding, it'll be most imperceptible.
i'm using something similar to make sure my popover doesn't go offscreen. you can see the slight slide.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | godbout |




