How do I make an About window the front-most windo

2020-07-13 09:38发布

问题:

I'm building a Cocoa application that runs as an item in the status bar. This application has an About window and an item to activate that about window, using the standard Cocoa mechanism for doing so (-[NSApplication orderFrontStandardAboutPanel:]). Naturally this is all hooked up automagically.

It works great except for one thing: unlike most About windows, it shows up underneath all other windows, rather than on top. I believe this is because the application does not have a UI, so all its windows are automatically beneath other windows. Is there a way I can hook into the NSApplication mechanism for displaying the About window so I can send it to the front, and make it respond to ⌘-W so it can be closed from the keyboard? I've poked around in the docs for NSApplication, but there's no way to get a reference to the About window that I can see so that I can make it appear on top.

回答1:

Is there a way I can hook into the NSApplication mechanism for displaying the About window so I can send it to the front?

That's what orderFrontStandardAboutPanel: does.

It works great except for one thing: unlike most About windows, it shows up underneath all other windows, rather than on top. I believe this is because the application does not have a UI, so all its windows are automatically beneath other [applications'] windows.

Try bringing your application to the front with [NSApp activateIgnoringOtherApps:YES].

Note that how you make your application “not have a UI” matters: If you're using LSUIElement, that will work, whereas LSBackgroundOnly pretty strictly means background-only.