Xcode 10 - NSVisualEffectView 在模态显示时添加到 NSWindow (Mac OS)

莱德马科

我有一个 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
沃伦·伯顿

您可以通过使窗口无边框来恢复您的非磨砂外观。

在此处输入图片说明

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

@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,现在通过使用具有自NSBox定义颜色的自定义样式或使用backgroundColor窗口上的属性可以更好地获取 backgroundColors

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章