0

I have succesfully changed the background NSColor of my NSTableView using setBackgroundColor: for both the NSTableView and the NSCell (using tableView: willDisplayCell:forTableColumn:row:). Now, I would like the background to also have this color when scrolling out (either top or bottom), which is currently white as pictured below.

enter image description here

I found this link which seems to indicate the need to subclass NSTableView and implement
- (void)drawBackgroundInClipRect:(NSRect)clipRect, any suggestion on that ?

Source : Themeing NSTableView

Bertrand Caron
  • 2,525
  • 2
  • 22
  • 49

2 Answers2

1

I finally found the answer at the question by looking at the header file for the NSTableView class NSTableView.h and found a mention to this method :

/* Override to customize background drawing.
*/
- (void)drawBackgroundInClipRect:(NSRect)clipRect;

I then subclassed NSTableView and overrode the previous method :

@implementation MyColoredTableView

/**
 Sets the color of the background for elastic scrollers
*/
- (void)drawBackgroundInClipRect:(NSRect)clipRect
{
    [[NSColor redColor]set];    //Set your color here
    NSRectFill(clipRect);
}

@end

Change the class of your NSTableView in InterfaceBuilder to MyColoredTableView, and you are done.

Bertrand Caron
  • 2,525
  • 2
  • 22
  • 49
0

> Now, I would like the background to also have this color when > scrolling out (either top or bottom),

For this you need to have one method which will called when you scroll your scroll view. So for that just include this notification below:-

-(void)viewDidLoad
{

// This notification will called when you scroll your scrollview
[[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification 
object:[yourScrollView contentView]];
}

-(void)boundsDidChange:(NSNotification*)not
{
//Write your code for changing background colors of tableview
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56