OSX El Capitan 上的透明 NSWindow

Transparent NSWindow on OSX El Capitan

在 El Capitan 之前,此代码可以正常工作。现在我的window不再是透明的了,它是白色的。

 NSWindow* window = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained defer:NO];
 [window setOpaque:NO];
 [window setBackgroundColor:[NSColor clearColor]];

有什么想法吗?感谢您的帮助。

我不确定你把代码放在哪里,但下面的代码对我有用。

  • 创建一个NSWindow子类
  • 插入以下代码:

-

- (id)initWithContentRect:(NSRect)contentRect
            styleMask:(NSUInteger)aStyle
              backing:(NSBackingStoreType)bufferingType
                defer:(BOOL)flag {

    // Using NSBorderlessWindowMask results in a window without a title bar.
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
    if (self != nil) {        
        // Start with no transparency for all drawing into the window
        [self setAlphaValue:1.0];
        //Set backgroundColor to clearColor
        self.backgroundColor = NSColor.clearColor;
        // Turn off opacity so that the parts of the window that are not drawn into are transparent.
        [self setOpaque:NO];
    }
    return self;
 }

此代码来自 Apples 示例代码页 - Round Transparent Window

您使用 NSBackingStoreNonretained 作为 window 支持。根据文档:

You should not use this mode. It exists primarily for use in the original Classic Blue Box. It does not support Quartz drawing, alpha blending, or opacity. Moreover, it does not support hardware acceleration, and interferes with system-wide display acceleration. If you use this mode, your application must manage visibility region clipping itself, and manage repainting on visibility changes.

所以根本不支持透明windows.