3
votes

I have an borderless subclass of NSWindow with custom graphics with rounded corners:

MyCustomWindow:

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation 
{ 
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
    if (self) {
        // 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 setMovableByWindowBackground:YES];
    }
    return self;
}

- (BOOL) canBecomeKeyWindow
{
    return YES;
}

MyCustomView:

- (void)drawRect:(NSRect)rect {
    [[NSColor clearColor] set];
    NSRectFill([self frame]);
    [backgroundImage compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
}

However, every once in a while (maybe 1 out of 10) when I start the application the graphics looks wrong because I get an grey one-pixel square border around the window. It is not set around my custom graphics but around the window's frame, which means that it negates my round corners.

Is there something I am missing in my subclasses?

EDIT: Here is a screenshot of the issue:

enter image description here

1
Seems I had the same problem and I got it fixed. See here: stackoverflow.com/questions/9124349/…sudo rm -rf
Did you ever figure this out? I have the same problem.sam
Sorry Sam, I didn't. I ended up using a less customized design where the window edges were kept as standard.pajevic

1 Answers

0
votes

You need to set background color to transparent color. Add this to Your MyCustomWindow:

[self setBackgroundColor:[NSColor clearColor]];

Your MyCustomWindow should look like this:

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation 
{ 
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
    if (self) {
        // Start with no transparency for all drawing into the window
        [self setAlphaValue:1.0];

        [self setBackgroundColor:[NSColor clearColor]];

        // Turn off opacity so that the parts of the window that are not drawn into are transparent.
        [self setOpaque:NO];
        [self setMovableByWindowBackground:YES];
    }
    return self;
}

- (BOOL) canBecomeKeyWindow
{
    return YES;
}

UPDATE:

Try edit Your drawRect:

Replace it with:

- (void)drawRect:(NSRect)rect {
    NSBezierPath * path;
    path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:6 yRadius:6];
    [[NSColor redColor] set];
    [path fill];

    /* If this example will help You. Replace redColor to clearColor and use this instead RectFill */
}

*Also add in Your MyCustomWindow [self setBackgroundColor:[NSColor clearColor]]; what I was said earlear.

Still with border?

It's working for me.