14

IS it possible to cut out parts of a NSWindow or an NSView and make them see through? I have an NSWindow with an NSView and I want to either:

A) make a hole in the NSWindow to be able to see through it or

B) set my NSWindow background to have a clear color and then make an NSView on top and set a certain part of my NSViews opacity to be able to see through to the desktop.

This is the effect I am trying to create:

enter image description here

1

1 Answer 1

26

Yes, it's possible, and actually not that hard.

In your window subclass, you need to set the window background color to transparent

self.backgroundColor = NSColor.clearColor;

and tell the compositing engine that parts of your window are transparent and need to be redrawn when the window moves

[self setOpaque:NO];

Setting the background color was not necessary in early versions of macOS and many answers still do not mention that fact. I've verified that at least since macOS 10.11 it is necessary.

In your NSView subclass, you must render the new background with a color of your choice (otherwise the window is entirely transparent and only the title bar will show) and then render a hole in the view with

NSRectFillUsingOperation(NSMakeRect(50, 50, 100, 100), NSCompositingOperationClear);

This gives the desired effect and also works in Mojave's dark mode etc.

window with hole in dark mode

Full code:

@interface MyWindow : NSWindow
    - (id)initWithContentRect:(NSRect)contentRect styleMask:( unsigned    int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag;
@end

@implementation MyWindow
   - (id)initWithContentRect:(NSRect)contentRect styleMask:( unsigned   int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
        self = [super initWithContentRect:contentRect styleMask : aStyle backing :bufferingType defer:flag ];
        if (self)
        {
            self.backgroundColor = NSColor.clearColor;
            [self setOpaque:NO];
            [self setHasShadow:NO];
        }
        return self;
}
@end

@interface MyView : NSView
- (void)drawRect:(NSRect)rect;
@end

@implementation MyView
- (void)drawRect:(NSRect)rect
{
    [[NSColor windowBackgroundColor] set];
    NSRectFill(self.bounds);
    NSRectFillUsingOperation(NSMakeRect(50, 50, 100, 100), NSCompositingOperationClear);
}
@end
5
  • Could I still use NSCompositeClear if i wanted to keep the first view and add a second view or a sub view? Is it possible to see through a view and also a window? Apr 2, 2012 at 5:22
  • 1
    @GrantWilkinson As this screenshot shows, NSCompositeClear in the drawing code of the superview will not draw through subviews. NSCompositeClear in the drawing code of the subview, however, will draw through its superview.
    – spudwaffle
    Apr 2, 2012 at 17:24
  • 1
    What is missing in the answer. 1.window background color should be 0.01 alpha value so it gets events. 2. Custom subview you can fill with color and after use composition operation to clear rectangle where you want transparency
    – Marek H
    Mar 4, 2018 at 14:36
  • I missed the hasShadow on MyWindow. This was what fixed everything for me. (I wasn't concerned with the shadow at the time.) It seems like the MyView.drawRect was only punching through on first draw. When I redrew my hole elsewhere, the backing window doesn't seem to update the hole. Toggling fixes the problem. Thank you!
    – waggles
    Feb 27, 2019 at 23:08
  • 1
    This is a good answer. The only problem is that the window's titlebar misbehaves in dark mode. It has an NSVisualEffectView that only looks right when it has the window background behind it. If you activate the app while the titlebar is over a bright spot, it remembers that and carries it around. I don't have a solution, but I thought others should know.
    – George
    Jun 29, 2020 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.