PDA

View Full Version : Filling gradient in NSTableHeaderView not working properly




Monaj
Dec 13, 2011, 08:26 AM
Hi all,

I am trying to fill gradient in header of tableview. So far I am able to achieve it by subclassing NSTableHeaderView and using this code in it-

- (void)drawRect:(NSRect)dirtyRect {
// Drawing code here.

NSGradient *gradientToFill = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:0.8828125 green:0.8984375 blue:0.91015625 alpha:1.0]
endingColor:[NSColor colorWithCalibratedRed:0.5546875 green:0.59765625 blue:0.66015625 alpha:1.0] ];
[gradientToFill drawInRect:dirtyRect angle:90];
}

So earlier it was appearing like this-

http://db.tt/PVE9rnXu


Now it is appearing like this-

http://db.tt/D1zrJJJa


As shown in above screen-shot, when I used this code I faced few problems:

1. Column header titles are not getting displayed.
2. Header column separators are not appearing.

Can anyone suggest me how to resolve these problems or some better way to implement it?

Thanks !



robbieduncan
Dec 13, 2011, 08:36 AM
I can't see the images as they are hosted off MR at a site which is blocked where I am. But as a first suggestion: what is dirtyrect is not the entire header? Your gradient will then be drawn to fill a smaller area so will not look right. You need to draw the entire height every time...

Monaj
Dec 13, 2011, 09:39 AM
You need to draw the entire height every time...

Can you suggest me some sample code ?

robbieduncan
Dec 13, 2011, 10:04 AM
Sorry, I don't do sample code. I have found through experience this translates as "can you post code I can copy into my application without having to understand it". I have told you what you one potential solution. As I said I can't be sure this is the problem as I can't see the images as you have chosen to host them off this site. The solutions I have suggested should be simple to understand if you have read the documentation on custom views and understand the code you already have.

JoshDC
Dec 13, 2011, 12:16 PM
The issue you're seeing isn't because dirtyRect is a different height to the view's bounds (although it may cause other issues later).

Think about what you're doing by overriding the drawRect: method with what you have. Your method only draws a gradient. What you're seeing is correct.

Have a look at NSTableHeaderCell (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableHeaderCell_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40004123) for how to override only certain parts of the drawing process.

Monaj
Dec 13, 2011, 11:47 PM
Have a look at NSTableHeaderCell (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableHeaderCell_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40004123) for how to override only certain parts of the drawing process.

I will definitely look at it... thanx for guiding me towards the correct path :)