0

我有一个 NSWindow,我以模态方式显示在其他窗口上以显示警报。代码是:

// This code is in MyAlert .m file. MyAlert inherits from NSWindow
- (void)showInWindow:(NSWindow *) {
    [window beginSheet:self completionHandler:NULL];
}

问题是,当在运行 Mojave 的 Mac 中使用 Xcode 10.1 编译时,我看到警报后面有一个灰色的“模糊”视图,这是我不希望出现的:我希望显示它的背景窗口可见。

使用 Xcode 9.4.1 编译的相同代码不会显示此模糊视图。

此外,我调试了 UI,确实在 Xcode 10.1 编译版本中插入了一个 NSVisualEffectView,在 9.4.1 上编译时不存在,但我似乎找不到摆脱它的方法。下面是两个版本中调试的 UI 截图。

Xcode 9 UI,没有模糊

Xcode 10 UI,模糊

有人面对并解决了这个问题吗?

更新(插入 nsvisualeffectview 的最小项目重现问题)http ://s000.tinyupload.com/?file_id=43114618701497778758

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{
  // Insert code here to initialize your application
  __weak typeof(self)weakSelf = self;
  NSView *contentView = self.window.contentView;
  contentView.wantsLayer = YES;
  [self.window setOpaque:NO];
  [self.window setHasShadow:NO];
  contentView.layer.backgroundColor = [NSColor redColor].CGColor;
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    __strong typeof(weakSelf) strongSelf = weakSelf;
    [strongSelf showAlert];
  });
}

- (void)showAlert {
  DummyAlertWindowController *alertController = [[DummyAlertWindowController alloc] initWithWindowNibName:@"DummyAlertWindowController"];
  [self.window beginSheet:alertController.window completionHandler:^(NSModalResponse returnCode) {
;
}];
}

@implementation DummyAlertWindowController

- (void)windowDidLoad {
    [super windowDidLoad];
  self.properContentView.wantsLayer = YES;
  self.properContentView.layer.backgroundColor = [NSColor blueColor].CGColor;
  [self.window setOpaque:NO];
  [self.window setHasShadow:NO];
  self.window.contentView.wantsLayer = YES;
  self.window.contentView.layer.backgroundColor = [NSColor clearColor].CGColor;
}

@end
4

1 回答 1

2

您可以通过使窗口无边框来恢复未结霜的外观。

在此处输入图像描述

将此类添加到您的项目中。

@interface BorderlessWindow : NSWindow

@end

@implementation BorderlessWindow

- (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSWindowStyleMask)style backing:(NSBackingStoreType)backingStoreType defer:(BOOL)flag {

    self = [super initWithContentRect:contentRect
                            styleMask:NSWindowStyleMaskBorderless
                              backing:backingStoreType
                                defer:flag];

    return self;
}

@end

并将您的窗口类设置XIBBorderlessWindow

最后backgroundColor在窗口上设置以获得透明度。

@implementation DummyAlertWindowController

- (void)windowDidLoad {
    [super windowDidLoad];
    //stuff
    [self.window setOpaque:NO];
    [self.window setHasShadow:NO];
    [self.window setBackgroundColor:[NSColor clearColor]];
}

@end

作为附带说明,现在通过使用具有自定义颜色的自定义样式或使用窗口上的属性,wantsLayer可以更好地使用获取 backgroundColors 。NSBoxbackgroundColor

于 2018-12-10T22:46:35.730 回答