I am developing a menubar-only application for OS X and I am struggeling to get a settings window to show up in front of other apps.
"Menubar-only application" means:
NSWindowController
in the main storyboard file. The main storyboard's window is not used in my appNSMenu
to the "Application Scene" in the main storyboard. This will become my menubar menuLSUIElement
to YES
to hide the dock iconLSBackgroundOnly
to NO
(see NSWindow makeKeyAndOrderFront makes window appear, but not Key or Front)When the app starts, I create an NSStatusItem
and add the NSMenu
from the storyboard as its menu. This all works fine - the app starts, shows no window and no dock icon but a menubar item that contains the menu from the storyboard.
I now wanted to add a settings window that is shown when a menubar entry is clicked. I therefore:
NSWindow
to itNSWindowController
that connects the outlets and actionsNSWindowController
using initWithNibNamed:
on app launchWhen the "Settings"-entry from the menu is clicked, I then try to bring the settings window to front using:
[self.settingsWindowController.window center];
[self.settingsWindowController.window showWindow:self];
[self.settingsWindowController.window makeKeyAndOrderFront:NSApp];
The window is shown, but not brought to the front but rather hidden behind other apps.
Any ideas how to solve this? Thanks!
You need to call:
[NSApp activateIgnoringOtherApps:YES];
(This is one of the rare occasions where it's correct to pass YES
to that method.)
For Swift you can use
NSApp.activate(ignoringOtherApps: true)