I open a NSWindow from my main NSWindow.
DropHereWindowController *dropHereWindowController = [[DropHereWindowController alloc] initWithWindowNibName:@"DropHereWindow"];
[dropHereWindowController showWindow:nil];
I want this window to stay on top of my main window when dragging a file from the finder to that "DropHereWindow". However when opening the finder (not having the focus any longer) my "DropHereWindow" goes behind my main window.
I tried orderFront, makeKey, makeKeyAndFront but nothing helped. What can I do about it?
NSStatusWindowLevel
works, just use it after some small delay after starting the app (or after creating the window).Method:
Sub-class the NSWindow:
Or simply use:
Available levels:
Swift 4.0, Xcode 9.0
You can set the
level
property of yourNSWindow
tofloating
. For example, if you are subclassingNSWindow
, you can set it in the override init.You also can get
NSWindow
from yourNSWindowController
byself.window?
.There are different levels:
Using the
CGWindowLevelKey
kCGMaximumWindowLevelKey
also works.CGWindowLevelKey Reference
You say:
And then:
Then you're doing something wrong.
For one thing, a window shouldn't automatically go behind another window anyway. Either you're (or the user is) ordering the main window front or you're ordering the other window back. I'll assume you're not doing the latter.
For another,
orderFront:
,makeKeyAndOrderFront:
, andsetLevel:
do work. In particular,setLevel:
puts the window on an entire other plane, so it will always be in front of (or behind, depending on the level you choose) windows with the default level, no matter what you do.I would guess that you have not hooked up, or you have accidentally disconnected, your
window
outlet to the window, which would mean you are sending yourorderFront:
/setLevel:
messages tonil
, which does nothing. Make sure your outlet is filled in at the point where you send theorderFront:
orsetLevel:
message, by logging the window to the console. If it says “(null)” or “0x0” (depending on how you log it), then your outlet holdsnil
; check that it's hooked up in the nib and that you've already loaded the nib/instantiated the window controller.All that said, I disagree that
setLevel:
is the correct solution. If you just want to have one window stay in front of a specific other window, and not put it on an entire other plane, make it a child window.This one worked for me, hope that can be helpful