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

问题描述

问题:我有一个窗口 mainWindow 和另一个 childWindow 添加到 mainWindow.childWindow 是一种 WindowExt 类.我定义了这个类,用于调用必须在 [childWindow makeKeyWindow] 之后调用的 catch 方法调用 [NSWindow becomeKeyWindow].如果我创建 childWindow 并尝试在下一个方法中使其成为 keyWindow:

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 被调用 - 一切正常,childWindow成为 keyWindow.

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

但是如果我创建 childWindow 为

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];

如果没有 NSTitledWindowMaskchildWindow[WindowExt becomeKeyWindow] 永远不会被调用 - childWindow 不会成为 keyWindow.

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

推荐答案

那是 Cocoa 的设计决策:没有标题或调整大小栏的窗口默认不能成为关键窗口.

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

如果想让一个无标题窗口成为关键窗口,则需要创建一个NSWindow的子类并重写-canBecomeKeyWindow如下:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 07:35