I'm trying to show a detachable NSPopover by clicking on a button but I'm stuck. I followed tuts how to show NSPopover but they all around Menubar apps.
My AppDelegate looks like this
final class AppDelegate: NSObject, NSApplicationDelegate {
var popover: NSPopover!
func applicationDidFinishLaunching(_ notification: Notification) {
let popover = NSPopover()
let popoverView = PopoverView()
popover.contentSize = NSSize(width: 300, height: 200)
popover.contentViewController = NSHostingController(rootView: popoverView)
popover.behavior = .transient
self.popover = popover
}
func togglePopover(_ sender: AnyObject?) {
self.popover.show(relativeTo: (sender?.bounds)!, of: sender as! NSView, preferredEdge: NSRectEdge.minY)
}
}
Here is a simple demo of possible approach - wrap control over native
NSPopoverinto background view representable.Note: next wrapping of background into view modifier or/and making it more configurable is up to you.
Prepared & tested with Xcode 13 / macOS 11.5.1