保持 NSWindow 最前面

发布于 2024-10-24 08:55:32 字数 398 浏览 6 评论 0原文

我从主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

橪书 2024-10-31 08:55:32

方法:

- (void)setLevel:(NSInteger)windowLevel

子类化 NSWindow:

[self setLevel: NSStatusWindowLevel];

或者简单地使用:

[window setLevel: NSStatusWindowLevel];

可用级别:

  • NSNormalWindowLevel
  • NSFloatingWindowLevel
  • NSSubmenuWindowLevel
  • NSTornOffMenuWindowLevel
  • NSModalPanelWindowLevel
  • NSMainMenuWindowLevel
  • NSStatusWindowLevel
  • NSPopUpMenuWindowLevel
  • NSScreenSaverWindowLevel
  • kCGDesktopWindowLevel

Method:

- (void)setLevel:(NSInteger)windowLevel

Sub-class the NSWindow:

[self setLevel: NSStatusWindowLevel];

Or simply use:

[window setLevel: NSStatusWindowLevel];

Available levels:

  • NSNormalWindowLevel
  • NSFloatingWindowLevel
  • NSSubmenuWindowLevel
  • NSTornOffMenuWindowLevel
  • NSModalPanelWindowLevel
  • NSMainMenuWindowLevel
  • NSStatusWindowLevel
  • NSPopUpMenuWindowLevel
  • NSScreenSaverWindowLevel
  • kCGDesktopWindowLevel
多情出卖 2024-10-31 08:55:32

Swift 4.0、Xcode 9.0

您可以将 NSWindowlevel 属性设置为 floating。例如,如果您要子类化 NSWindow,则可以在覆盖 init 中设置它。

self.level = .floating

您还可以通过 self.window?NSWindowController 获取 NSWindow

有不同的级别:

NSNormalWindowLevel

NSWindow 对象的默认级别。

NSFloatingWindowLevel

对于浮动调色板很有用。

NSSubmenuWindowLevel

保留用于子菜单。与 NSTornOffMenuWindowLevel 同义,这是首选。

NSTornOffMenuWindowLevel

撕下菜单的级别。与 NSSubmenuWindowLevel 同义。

NSModalPanelWindowLevel

模式面板的级别。

NSMainMenuWindowLevel

为应用程序的主菜单保留。

NSStatusWindowLevel

状态窗口的级别。

NSPopUpMenuWindowLevel

弹出菜单的级别。

NSScreenSaverWindowLevel

屏幕保护程序的级别。

Swift 4.0, Xcode 9.0

You can set the level property of your NSWindow to floating. For example, if you are subclassing NSWindow, you can set it in the override init.

self.level = .floating

You also can get NSWindow from your NSWindowController by self.window?.

There are different levels:

NSNormalWindowLevel

The default level for NSWindow objects.

NSFloatingWindowLevel

Useful for floating palettes.

NSSubmenuWindowLevel

Reserved for submenus. Synonymous with NSTornOffMenuWindowLevel, which is preferred.

NSTornOffMenuWindowLevel

The level for a torn-off menu. Synonymous with NSSubmenuWindowLevel.

NSModalPanelWindowLevel

The level for a modal panel.

NSMainMenuWindowLevel

Reserved for the application’s main menu.

NSStatusWindowLevel

The level for a status window.

NSPopUpMenuWindowLevel

The level for a pop-up menu.

NSScreenSaverWindowLevel

The level for a screen saver.

骷髅 2024-10-31 08:55:32

你说:

我尝试了 orderFront、makeKey、makeKeyAndFront 但没有任何帮助。

进而:

<块引用>

方法:

- (void)setLevel:(NSInteger)windowLevel

它不起作用,当单击查找器图标时,窗口仍然位于我的主窗口后面。

那你就做错事了。

一方面,一个窗口无论如何都不应该自动位于另一个窗口后面。您(或用户)要么将主窗口订购在前面,要么将另一个窗口订购在后面。我假设你不会做后者。

对于另一个,orderFront:makeKeyAndOrderFront:setLevel:起作用。特别是,setLevel:将窗口放在整个其他平面上,因此它将始终位于前面(或后面,具体取决于无论您做什么,都具有默认级别的窗口。

我猜想您没有连接或意外断开了 window 出口到窗口的连接,这意味着您正在发送 orderFront:/setLevel: 消息发送到 nil,它什么也不做。通过将窗口记录到控制台,确保在发送 orderFront:setLevel: 消息时已填写您的插座。如果它显示“(null)”或“0x0”(取决于您记录的方式),那么您的插座将保存nil;检查它是否已连接在笔尖中,并且您已经加载笔尖/实例化窗口控制器。

尽管如此,我不同意 setLevel: 是正确的解决方案。如果您只想让一个窗口停留在另一个特定窗口的前面,而不是将其放在整个其他平面上,将其设为子窗口

You say:

I tried orderFront, makeKey, makeKeyAndFront but nothing helped.

And then:

Method:

- (void)setLevel:(NSInteger)windowLevel

It doesn't work, the window still goes behind my main window when clicking on the finder icon.

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:, and setLevel: 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 your orderFront:/setLevel: messages to nil, which does nothing. Make sure your outlet is filled in at the point where you send the orderFront: or setLevel: message, by logging the window to the console. If it says “(null)” or “0x0” (depending on how you log it), then your outlet holds nil; 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.

楠木可依 2024-10-31 08:55:32

这对我有用,希望能有所帮助

[self.window makeKeyAndOrderFront:nil];
[self.window setLevel:NSStatusWindowLevel];

This one worked for me, hope that can be helpful

[self.window makeKeyAndOrderFront:nil];
[self.window setLevel:NSStatusWindowLevel];
想念有你 2024-10-31 08:55:32

NSStatusWindowLevel 有效,只需在启动应用程序(或创建窗口后)后稍有延迟后使用即可。

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
    self.view.window.level = NSStatusWindowLevel;
});

NSStatusWindowLevel works, just use it after some small delay after starting the app (or after creating the window).

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
{
    self.view.window.level = NSStatusWindowLevel;
});
探春 2024-10-31 08:55:32

使用 CGWindowLevelKey kCGMaximumWindowLevelKey 也可以。

[window setLevel:CGWindowLevelForKey(kCGMaximumWindowLevelKey)];

CGWindowLevelKey参考

Using the CGWindowLevelKey kCGMaximumWindowLevelKey also works.

[window setLevel:CGWindowLevelForKey(kCGMaximumWindowLevelKey)];

CGWindowLevelKey Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文