【问题标题】:Set focus of NSWindow on top, even if changing of virtual desk即使更改虚拟桌面,也将 NSWindow 的焦点设置在顶部
【发布时间】:2016-11-30 17:19:47
【问题描述】:

我正在做一个应用程序,该应用程序包括在一定时间后提出一个问题,并且用户不能再做任何事情,除非他回答问题,我已经设法通过设置一个9999级:

[_idleWindow setLevel:9999];

我想知道是否有办法避免在窗口打开时更改虚拟桌面,或者在更改虚拟桌面时重新聚焦窗口。

【问题讨论】:

    标签: objective-c xcode macos nswindow


    【解决方案1】:

    看看setCollectionBehavior:

    [_idleWindow setCollectionBehavior:NSWindowCollectionBehaviorMoveToActiveSpace|NSWindowCollectionBehaviorTransient|NSWindowCollectionBehaviorFullScreenDisallowsTiling|NSWindowCollectionBehaviorFullScreenAuxiliary];
    

    可能会成功。

    此外,如果您可以使用 NSPanel 代替 NSWindow,您可以添加样式掩码:NSWindowStyleMaskNonactivatingPanel 以提供您的窗口键状态,即使您的应用程序未处于活动状态。 (您需要在 NSPanel 子类中实现 canBecomeKeyWindow)

    【讨论】:

    • 我刚刚有时间测试您的解决方案,但它不起作用,将测试 NSPanel
    【解决方案2】:

    在谷歌上查了几下,在另一个Stackoverflow post上找到了解决办法。

    解决方案:

    You need to add this code either in your xib / storyboard, either in your NSWindowController subclass -windowDidLoad method, either in your designated initialiser of your NSWindow subclass :
    
    - (void) awakeFromNib { 
        [self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces]; //this was the one that worked for me
    }
    OR
    
    - (id)initWithContentRect:(NSRect)contentRect
                    styleMask:(NSUInteger)styleMask
                      backing:(NSBackingStoreType)bufferingType
                        defer:(BOOL)flag {
        if (self = [super initWithContentRect:contentRect styleMask:styleMask  backing:bufferingType defer:flag]) {
            [self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
        }
        return self;
    }
    OR if you have a NSWindowController
    
    - (void)windowDidLoad {
        [super windowDidLoad];
    
        [[self window] setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
    }
    OR Edit the nib file and add this behaviour to your window in XCode.
    

    致谢:Stefan Szekeres

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2021-12-07
      • 1970-01-01
      • 2011-06-19
      • 2021-06-09
      相关资源
      最近更新 更多