OSX El Capitan上的透明NSWindow

jun*_*ans 4 cocoa transparency nswindow osx-elcapitan

在El Capitan之前,这段代码正是如何应用的.现在我的窗户不再透明,它是白色的.

 NSWindow* window = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained defer:NO];
 [window setOpaque:NO];
 [window setBackgroundColor:[NSColor clearColor]];
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?谢谢您的帮助.

man*_*ahn 6

我不确定你把代码放在哪里,但以下对我有用.

  • 创建一个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;
 }
Run Code Online (Sandbox Code Playgroud)

此代码来自Apples示例代码页 - 圆形透明窗口

你用作NSBackingStoreNonretained窗口背衬.根据文件:

您不应该使用此模式.它主要用于原始的经典蓝盒子.它不支持 Quartz绘图,alpha混合或不透明度.此外,它不支持硬件加速,并且干扰系统范围的显示加速.如果使用此模式,则应用程序必须管理可见区域剪裁本身,并管理可见性更改的重新绘制.

所以它根本不支持透明窗口.