0

I am building a macOS menubar app in Swift, using an NSStatusItem, which opens an NSPopover when the NSStatusItem's button is clicked.

How can I also give the NSPopover focus? Currently, the user needs to click on the popover to focus it, but I want to grab focus programmatically.

Thanks in Advance

Vishal Parmar
  • 524
  • 7
  • 27
j b
  • 5,147
  • 5
  • 41
  • 60

1 Answers1

0

The solution is to call makeKey() on the owning window.

This can be done either from the main NSApplicationDelegate, e.g.

func applicationDidFinishLaunching(_ aNotification: Notification) {

     // Other setup

     popover.contentViewController?.view.window?.makeKey()
}

Or from the relevant NSViewController, e.g.

override func viewDidAppear() {
    super.viewDidAppear()
    view.window?.makeKey()
}

Documentation is here

j b
  • 5,147
  • 5
  • 41
  • 60