本文介绍了为什么NSWindow没有styleMask:NSTitledWindowMask不能是keyWindow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:
我有一个窗口mainWindow,另一个 childWindow 添加到 mainWindow
childWindow是一种 WindowExt 类。这个类定义为catch方法调用 [NSWindow becomeKeyWindow] ,必须在 [childWindow makeKeyWindow] 后调用。
如果我创建childWindow并试图让它在下一个keyWindow:

  WindowExt * childWindow = [[WindowExt alloc ] initWithContentRect:addedWindowRect 
styleMask:NSBorderlessWindowMask | NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[mainWindow addChildWindow:childWindow ordered:NSWindowAbove];
[childWindow makeKeyWindow];

方法 [WindowExt becomeKeyWindow] c $ c> childWindow 被调用 - 一切正常, childWindow 成为keyWindow。



但是如果我创建childWindow作为

  WindowExt * childWindow = [[WindowExt alloc] initWithContentRect:addedWindowRect 
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[mainWindow addChildWindow:childWindow ordered:NSWindowAbove];
[childWindow makeKeyWindow];

不含 NSTitledWindowMask ,,childWindow 不会被调用 - childWindow 不会成为keyWindow 。

解决方案

这是Cocoa设计决策:。 p>

如果你想要一个无标题窗口能够成为一个关键窗口,你需要创建一个 NSWindow 重写 -canBecomeKeyWindow 如下:

   - (BOOL)canBecomeKeyWindow {
return YES;
}


Problem:I have one window mainWindow and another childWindow added to mainWindow.childWindow is kind of WindowExt class. This class I define for catch method call [NSWindow becomeKeyWindow] that must be called after [childWindow makeKeyWindow].If I create childWindow and try to make it keyWindow on next way:

WindowExt *childWindow = [[WindowExt alloc] initWithContentRect:addedWindowRect
                           styleMask:NSBorderlessWindowMask | NSTitledWindowMask
                             backing:NSBackingStoreBuffered 
                               defer:NO];
[mainWindow addChildWindow:childWindow ordered:NSWindowAbove];
[childWindow makeKeyWindow];

method [WindowExt becomeKeyWindow] for childWindow is called - all fine, childWindowbecome keyWindow.

But if I create childWindow as

WindowExt *childWindow = [[WindowExt alloc] initWithContentRect:addedWindowRect
styleMask:NSBorderlessWindowMask 
backing:NSBackingStoreBuffered
defer:NO];
[mainWindow addChildWindow:childWindow ordered:NSWindowAbove];
[childWindow makeKeyWindow];

without NSTitledWindowMask, [WindowExt becomeKeyWindow] for childWindow is never called - childWindow doesn't become keyWindow.

解决方案

That’s a Cocoa design decision: windows without title or resize bar cannot become key window by default.

If you want a titleless window to be able to become a key window, you need to create a subclass of NSWindow and override -canBecomeKeyWindow as follows:

- (BOOL)canBecomeKeyWindow {
    return YES;
}

这篇关于为什么NSWindow没有styleMask:NSTitledWindowMask不能是keyWindow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 00:29