Problem: I am trying to create a custom transparent TableView Header and I have created subclasses of NSTableHeaderView and NSTableHeaderCell and overridden -drawWithFrame:inView and -drawInteriorWithFrame:inView in the NSTableHeaderCell subclass. These methods are working as expected, but only when the table column header is first drawn. After the user clicks on the table header, however, it is re-drawn with a white background. To get specific, here are the custom method implementations:

@interface MYTableHeaderCell : NSTableHeaderCell
@end

@implementation MYTableHeaderCell

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
//[super drawWithFrame:cellFrame inView:controlView];
NSBezierPath *path = [NSBezierPath bezierPathWithRect:cellFrame];
NSColor *clearColor = [NSColor clearColor];
[clearColor setFill];
[path fill];
[self drawInteriorWithFrame:cellFrame inView:controlView];
}

-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
//[super drawInteriorWithFrame:cellFrame inView:controlView];
NSBezierPath *path = [NSBezierPath bezierPathWithRect:cellFrame];
NSColor *clearColor = [NSColor clearColor];
[clearColor setFill];
[path fill];
NSRect titleRect = [self titleRectForBounds:cellFrame];
[self.attributedStringValue drawInRect:titleRect];
}

-(NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
return [NSColor clearColor];
}

-(BOOL)isOpaque{
return NO;
}

When the table view header is first drawn, it has a transparent background as intended. After clicking on the header, however, it is redrawn to have a white background.

As far as I can tell, after clicking on the table view header

-drawInteriorWithFrame:inView: 

is still called when the header needs to be drawn. However,

-drawWithFrame:inView: 

is not. It also appears that another class is drawing a white view underneath the cell text.

I have looked through the NSTableHeaderCell and NSTableHeaderView class descriptions along with all of their superclasses but I can't figure out why the white background is being drawn. I'm obviously missing something fundamental.

Question: What is causing the white view to be drawn?

1

There are 1 answers

0
Andreas Utzinger On

The

highlight(flag: Bool, withFrame cellFrame: NSRect, inView controlView: NSView)

method is not implemented. Override this method and copy your code from your drawRect method in there and this should be done.