CodexBloom - Programming Q&A Platform

NSPopover Not Dismissing When Clicking Outside on macOS 13.6

👀 Views: 124 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-23
macos swift nspopover Swift

I've been banging my head against this for hours. I'm experiencing an scenario with `NSPopover` in my macOS application running on macOS 13.6. I've implemented a custom popover that shows when a button is clicked, but it doesn't dismiss when I click outside of the popover. I have set the `behavior` of the popover to `.transient`, expecting it to close automatically when clicking outside, but it stays open. Here's a simplified version of my implementation: ```swift class ViewController: NSViewController { let popover = NSPopover() override func viewDidLoad() { super.viewDidLoad() popover.behavior = .transient popover.contentViewController = PopoverContentViewController() let button = NSButton(title: "Show Popover", target: self, action: #selector(showPopover(_:)) view.addSubview(button) } @objc func showPopover(_ sender: NSButton) { popover.show(relativeTo: sender.bounds, of: sender, preferredEdge: .maxY) } } ``` I also tried using `popover.performClose(nil)` in the `viewDidDisappear` of the popover's content view controller, but that didn't seem to have any effect. I checked to ensure that `NSPopover` is not in a modal state. I have a delegate set on the popover, but I'm not seeing any relevant delegate methods being called when I click outside. I need the popover to close when the user clicks anywhere outside of it, which is standard behavior for transient popovers. Is there a known workaround or fix for this scenario? Any guidance would be appreciated! I'm working on a web app that needs to handle this.