Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent NSWindow on OSX El Capitan

Before El Capitan, this code was working exactly how it should. Now my window is not transparent anymore, it's white.

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

Any ideas ? Thanks for the help.

like image 518
junghans Avatar asked Oct 15 '15 09:10

junghans


1 Answers

I am not sure where you put the code, but the following worked for me.

  • Create a NSWindow subclass
  • Insert the following code:

-

- (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;
 }

This code is from Apples Sample Code Page - Round Transparent Window

You used NSBackingStoreNonretained as the window backing. According to the docs:

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.

So it simply does not support transparent windows.

like image 115
mangerlahn Avatar answered Nov 15 '22 00:11

mangerlahn