OSX El Capitan上的透明NSWindow

时间:2015-10-15 09:27:25

标签: 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]];

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

1 个答案:

答案 0 :(得分: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;
 }

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

您使用NSBackingStoreNonretained作为窗口支持。根据文件:

  

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

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

相关问题