【问题标题】:Transparent NSWindow on OSX El CapitanOSX El Capitan 上的透明 NSWindow
【发布时间】:2015-10-15 09:27:24
【问题描述】:

在 El Capitan 之前,此代码完全按照应有的方式运行。现在我的窗口不再透明了,它是白色的。

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

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

【问题讨论】:

    标签: cocoa transparency nswindow osx-elcapitan


    【解决方案1】:

    我不确定您将代码放在哪里,但以下内容对我有用。

    • 创建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;
     }
    

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

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

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

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

    【讨论】:

    • 确实,您的代码可以工作,唯一的区别是 NSBackingStoreBuffered 作为支持。现在它适用于 El Capitan!谢谢!你能解释一下有什么区别吗?
    猜你喜欢
    • 2017-04-20
    • 2015-12-29
    • 1970-01-01
    • 2015-12-23
    • 2016-11-03
    • 2016-07-05
    • 2016-02-02
    • 2017-07-09
    • 2017-01-14
    相关资源
    最近更新 更多