How to bring NSWindow to front and to the current Space?

Viewed 27290

I'm using this code to bring up my window:

[self.window makeKeyAndOrderFront:self];
[self.window setOrderedIndex:0];

But often it will be beneath other windows or displayed in other Space desktop that I last open it in. How do I make it always appear "in front" to the user?

Update I found the answer from previously asked question; make the window show up to the top of other windows: [NSApp activateIgnoringOtherApps:YES];

But how to move the app to the user's current Space?

8 Answers

Swift 5.0 +

 NSWindow.orderFrontRegardless()

If you want to your app to come to the front, even if its minimised and have it activated (given active focus), then use both of these:

            NSApp.activate(ignoringOtherApps: true)
            NSApp.windows.first?.orderFrontRegardless()

If you only want to bring it to the front, without giving it focus, just use:

            NSApp.windows.first?.orderFrontRegardless()
Related