NSPopover - Hide when focus lost? (clicked outside of popover)

前端 未结 2 587
你的背包
你的背包 2021-02-13 01:41

I\'m using the doubleClickAction of a NSTableView to display a NSPopover. Something like this:

NSInteger selectedRow = [da         


        
相关标签:
2条回答
  • 2021-02-13 02:05

    You need to change the property behavior of your popover (in code or on interface builder) to:

    popover.behavior = NSPopover.Behavior.transient;
    

    NSPopover.Behavior.transient
    The system will close the popover when the user interacts with a user interface element outside the popover.

    Read more about this in Apple's documentation.

    0 讨论(0)
  • 2021-02-13 02:08

    the .transient flag doesn't work for me.

    However I can make things work by the following:

    1) Whenever I show my popover I make sure I activate the app (my app is a menu-bar app, so this doesn't happen automatically)

    NSApp.activate(ignoringOtherApps: true)
    

    2) When I click outside the app, then my app will be deactivated. I can detect this in the AppDelegate

    func applicationWillResignActive(_ notification: Notification) {
        print("resign active")
    }
    

    and act accordingly

    0 讨论(0)
提交回复
热议问题