objective-ccocoansviewquartz-2dnssegmentedcontrol

Strange drawing behavior of NSSegmentedControl, when superview is drawn


I've got a problem with the NSSegmentedControl. I have a custom view where I draw a gradient with a shadow. The drawing is ok, but when I place a NSSegmentedControl inside it then something strange happens - the background of the custom view is shining through the segmented control, as if the segmented control had a low alpha value.

Here are pictures, that demonstrate the issue :

With the custom view in background : enter image description here

Without the custom view in background : enter image description here

As you can see in the left upper corner in the first image the segmented control isn't drawing properly. Here is the drawRect method from the custom view :

- (void)drawRect:(NSRect)dirtyRect {

    NSShadow *shadow = [[NSShadow alloc] init];

    [shadow setShadowOffset:NSMakeSize(0.0, -6.0)];

    [shadow setShadowBlurRadius:3.0];

    [shadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.3]];

    [shadow set];



    NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations:[NSColor colorWithDeviceWhite:0.8 alpha:1.0],(CGFloat)0.0,[NSColor colorWithDeviceWhite:0.65 alpha:1.0],(CGFloat)0.5,[NSColor colorWithDeviceWhite:0.3 alpha:1.0],(CGFloat)0.5,[NSColor colorWithDeviceWhite:0.5 alpha:1.0],(CGFloat)1.0, nil];

    [gradient drawInRect:self.bounds angle:90];



    NSBezierPath *bezierPath = [NSBezierPath bezierPath];

    [bezierPath moveToPoint:NSMakePoint(0, 0)];

    [bezierPath lineToPoint:NSMakePoint(-self.bounds.size.height, 0)];

    [bezierPath lineToPoint:NSMakePoint(-self.bounds.size.height, -self.bounds.size.width)];

    [bezierPath lineToPoint:NSMakePoint(0, -self.bounds.size.width)];



    [[NSColor whiteColor] set];



    [bezierPath stroke];

}    

Can you help me with this? I don't know how to fix this issue.


Solution

  • On 10.7+, Rounded Textured segmented controls and buttons are transparent. On 10.5-10.6, they are gradient filled.