Question

If I remove this method from my view everything works fine (no content disappears if I click on a button), so this is definitely the cause.

I'm trying to make a window that is rounded and has a gradient via the code below. Is there anything wrong with this at all that could cause content on the view to disappear?

- (void)drawRect:(NSRect)dirtyRect
{
    [NSGraphicsContext saveGraphicsState];

    NSBezierPath *outerClip = [NSBezierPath bezierPathWithRoundedRect:[self bounds]
                                                              xRadius:3.0
                                                              yRadius:3.0];
    [outerClip setClip];

    NSGradient* aGradient = [[NSGradient alloc]
                             initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]
                             endingColor:[NSColor colorWithCalibratedWhite:0.65 alpha:1.0]];

    [aGradient drawInRect:[outerClip bounds] angle:270];

    [NSGraphicsContext restoreGraphicsState];

}

Was it helpful?

Solution

Switching to NSGradient drawInbezierPath fixes the issue.

OTHER TIPS

I know you answered your own question but I thought I would share why your content was vanishing with the above code.

When you use setClip you are removing the previous clipping path and replacing it with your new path. This means you will wind up drawing outside of the dirty area, thus overwriting previously drawn content.

I had the same issue with drawing rounded corners on my own splash screen, and eventually found a different way to do what I wanted.

Also, you can use the clipRect: class method of NSBezierPath to change the clipping path to the intersection of the existing path and the area you want to restrict drawing into. Of course, save and restore the graphics state around your call to clipRect:.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top