【发布时间】:2016-11-30 17:19:47
【问题描述】:
我正在做一个应用程序,该应用程序包括在一定时间后提出一个问题,并且用户不能再做任何事情,除非他回答问题,我已经设法通过设置一个9999级:
[_idleWindow setLevel:9999];
我想知道是否有办法避免在窗口打开时更改虚拟桌面,或者在更改虚拟桌面时重新聚焦窗口。
【问题讨论】:
标签: objective-c xcode macos nswindow
我正在做一个应用程序,该应用程序包括在一定时间后提出一个问题,并且用户不能再做任何事情,除非他回答问题,我已经设法通过设置一个9999级:
[_idleWindow setLevel:9999];
我想知道是否有办法避免在窗口打开时更改虚拟桌面,或者在更改虚拟桌面时重新聚焦窗口。
【问题讨论】:
标签: objective-c xcode macos nswindow
看看setCollectionBehavior:
[_idleWindow setCollectionBehavior:NSWindowCollectionBehaviorMoveToActiveSpace|NSWindowCollectionBehaviorTransient|NSWindowCollectionBehaviorFullScreenDisallowsTiling|NSWindowCollectionBehaviorFullScreenAuxiliary];
可能会成功。
此外,如果您可以使用 NSPanel 代替 NSWindow,您可以添加样式掩码:NSWindowStyleMaskNonactivatingPanel 以提供您的窗口键状态,即使您的应用程序未处于活动状态。 (您需要在 NSPanel 子类中实现 canBecomeKeyWindow)
【讨论】:
在谷歌上查了几下,在另一个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.
【讨论】: