Rounded NSView in a transparent window

I am trying to make a transparent NSWindow with a rounded view.

I am trying to get a rounded view with a transparent window.

This is what it now looks like: (see small dots in the corners)

enter image description here

Here is another example with a border radius set to 10px (set to NSView drawRect):

enter image description here

I use the code from this Apple example: https://developer.apple.com/library/mac/#samplecode/RoundTransparentWindow/Introduction/Intro.html

Specifically, this method in my subclass of 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]; // Turn off opacity so that the parts of the window that are not drawn into are transparent. [self setOpaque:NO]; [self setBackgroundColor:[NSColor clearColor]]; } return self; } 

And this is in my subclass of NSView:

 - (void)drawRect:(NSRect)dirtyRect { [[NSColor redColor] set]; NSBezierPath* thePath = [NSBezierPath bezierPath]; [thePath appendBezierPathWithRoundedRect:dirtyRect xRadius:3 yRadius:3]; [thePath fill]; } 

Can someone tell me what I am missing here?

Thanks.

+4
source share
2 answers

Are you looking for something like the following, where there is a red outline (stroke) but the center area is transparent?

enter image description here

If so, I used the following code for this:

 - (void)drawRect:(NSRect)frame { frame = NSInsetRect(self.frame, 3.0, 3.0); [NSBezierPath setDefaultLineWidth:6.0]; NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:6.0 yRadius:6.0]; [[NSColor redColor] set]; [path stroke]; } 

If this is what you are looking for, you can probably use this as a starting point. You need to make sure that you insert the frame rect one half the stroke line width to avoid the problem of clipping corners, as you saw.

+3
source

Not sure if this is what you are looking for, but there is a great class from Matt Gemmell called MAAttachedWindow and can be found here: http://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass/

It's a bit older, but still great for me when I need to make a popup with a floating point and adjust transparency, radius borders, and even add a small arrow for context if necessary. I use it all the time.

+3
source

All Articles