Keep NSWindow front

て烟熏妆下的殇ゞ 提交于 2019-12-18 19:08:30

问题


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?


回答1:


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



回答2:


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.




回答3:


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.




回答4:


This one worked for me, hope that can be helpful

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



回答5:


Using the CGWindowLevelKey kCGMaximumWindowLevelKey also works.

[window setLevel:CGWindowLevelForKey(kCGMaximumWindowLevelKey)];

CGWindowLevelKey Reference




回答6:


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;
});


来源:https://stackoverflow.com/questions/5364460/keep-nswindow-front

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!