保持 NSWindow 最前面
我从主 NSWindow 打开一个 NSWindow。
DropHereWindowController *dropHereWindowController = [[DropHereWindowController alloc] initWithWindowNibName:@"DropHereWindow"];
[dropHereWindowController showWindow:nil];
当将文件从查找器拖动到“DropHereWindow”时,我希望此窗口保留在主窗口的顶部。然而,当打开取景器(不再具有焦点)时,我的“DropHereWindow”位于我的主窗口后面。
我尝试了 orderFront、makeKey、makeKeyAndFront 但没有任何帮助。 我能做什么呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
方法:
子类化 NSWindow:
或者简单地使用:
可用级别:
Method:
Sub-class the NSWindow:
Or simply use:
Available levels:
Swift 4.0、Xcode 9.0
您可以将
NSWindow
的level
属性设置为floating
。例如,如果您要子类化NSWindow
,则可以在覆盖 init 中设置它。您还可以通过
self.window?
从NSWindowController
获取NSWindow
。有不同的级别:
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:
你说:
进而:
那你就做错事了。
一方面,一个窗口无论如何都不应该自动位于另一个窗口后面。您(或用户)要么将主窗口订购在前面,要么将另一个窗口订购在后面。我假设你不会做后者。
对于另一个,
orderFront:
,makeKeyAndOrderFront:
和setLevel:
起作用。特别是,setLevel:
将窗口放在整个其他平面上,因此它将始终位于前面(或后面,具体取决于无论您做什么,都具有默认级别的窗口。我猜想您没有连接或意外断开了
window
出口到窗口的连接,这意味着您正在发送orderFront:
/setLevel:
消息发送到nil
,它什么也不做。通过将窗口记录到控制台,确保在发送orderFront:
或setLevel:
消息时已填写您的插座。如果它显示“(null)”或“0x0”(取决于您记录的方式),那么您的插座将保存nil
;检查它是否已连接在笔尖中,并且您已经加载笔尖/实例化窗口控制器。尽管如此,我不同意
setLevel:
是正确的解决方案。如果您只想让一个窗口停留在另一个特定窗口的前面,而不是将其放在整个其他平面上,将其设为子窗口。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
NSStatusWindowLevel
有效,只需在启动应用程序(或创建窗口后)后稍有延迟后使用即可。NSStatusWindowLevel
works, just use it after some small delay after starting the app (or after creating the window).使用
CGWindowLevelKey
kCGMaximumWindowLevelKey
也可以。CGWindowLevelKey参考
Using the
CGWindowLevelKey
kCGMaximumWindowLevelKey
also works.CGWindowLevelKey Reference