objective-c – NSTextView的意外行为(因为标题栏少了NSWindow?)

我有一个NSWindow(我的主窗口)和一个子窗口(位于NSWindowBelow主窗口),它有一个NSTextView.子窗口没有标题栏,阴影也没有透明.

这是我用来设置子窗口以使其透明的代码:

- (id) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag{
    if (![super initWithContentRect: contentRect styleMask:NSBorderlessWindowMask backing: bufferingType defer:NO]) return nil;

    [self setBackgroundColor: [NSColor clearColor]];
    [self setOpaque:NO];

    return self;
}

但是当我尝试选择其中的文本时,会发生什么(子窗口上方的黑色内容是主窗口):

看起来NSTextView没有聚焦,因为选择不是蓝色.我试过调用:[[_ childWindow textView] becomeFirstResponder];但结果是一样的.另一件事是,当我滚动它时,有时它会非常迟钝和“破碎”.

你们有什么想法导致这个以及如何解决它?我怀疑是因为窗口没有标题栏,但我不确定.
谢谢!

最佳答案 来自NSWindow的文档:

canBecomeKeyWindow
Indicates whether the window can become the key window.

- (BOOL)canBecomeKeyWindow
Return Value
YES if the window can become the key window, otherwise, NO.

Discussion
Attempts to make the window the key window are abandoned if this method returns 
NO. The NSWindow implementation returns YES if the window has a title bar or a 
resize bar, or NO otherwise.

尝试重写-canBecomeKeyWindow并返回YES.

点赞