macos NSWindow makeKeyAndOrderFront 显示窗口,但不显示 Key 或 Front
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7460092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSWindow makeKeyAndOrderFront makes window appear, but not Key or Front
提问by ck_
makeKeyAndOrderFront
is not making my NSWindow key or front.
makeKeyAndOrderFront
不是制作我的 NSWindow 键或前面。
My app does not have a main window or menubar, which may be part of the problem?
我的应用程序没有主窗口或菜单栏,这可能是问题的一部分?
IBOutlet NSWindow *loginWindow;
//(connected in Interface Builder to the NSWindow)
The NSWindow has "visible at launch" and "release when closed" both unchecked.
NSWindow 没有选中“启动时可见”和“关闭时释放”。
Then:
然后:
- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
[loginWindow makeKeyAndOrderFront:self];
}
This doesopen the window. (commenting it out results in no window being opened).
这确实打开了窗口。(注释掉它不会打开任何窗口)。
However:
然而:
It appears behind the Xcode window (not front).
It appears to recieve mouse focus, but will not take keypresses in the textfields the window contains. The keypresses are sent to Xcode.
It appears in the Expose grid when Expose is activated. But I cannot click the window to select it in Expose... it won't come to the front.
它出现在 Xcode 窗口后面(不是前面)。
它似乎接收鼠标焦点,但不会在窗口包含的文本字段中进行按键操作。按键被发送到 Xcode。
当 Expose 被激活时,它会出现在 Expose 网格中。但是我无法在 Expose 中单击窗口来选择它……它不会出现在前面。
Why isn't my Window working?
为什么我的窗口不工作?
回答by ms83
Try calling this method [NSApp activateIgnoringOtherApps:YES];
. This should make it the active application.
尝试调用此方法[NSApp activateIgnoringOtherApps:YES];
。这应该使它成为活动的应用程序。
回答by Peter Hosey
Stab in the dark: You have LSBackgroundOnly
set in your Info.plist. That's what makes this not work: A background-only application cannot come to the foreground, which is why your window does not come to the foreground.
暗中刺伤:您已LSBackgroundOnly
在 Info.plist 中进行设置。这就是它不起作用的原因:仅后台应用程序无法进入前台,这就是您的窗口无法进入前台的原因。
If you want your app to not show up in the Dock, set LSUIElement
instead. This suppresses your app's Dock tile and keeps it from showing its own main menu in the menu bar, but preserves its ability to bring a window frontmost and make it key.
如果您不希望您的应用程序显示在 Dock 中,请LSUIElement
改为设置。这会抑制您的应用程序的 Dock 磁贴并阻止它在菜单栏中显示自己的主菜单,但保留其将窗口置于最前面并使其成为关键的能力。
回答by Sam Soffes
Both of these answers are correct. You'll also need to make sure you override canBecomeKey
in your window subclass if it's borderless.
这两个答案都是正确的。canBecomeKey
如果它是无边框的,您还需要确保在窗口子类中进行覆盖。
This took be forever to figure out. (I wrote a blog postabout the entirety of my solution.)