Gareth Jeanne
Gareth Jeanne

Reputation: 1420

Set Background colour on NSTableCellView

I am trying to set a background colour on an NSTableCellView, but there doesn't seem to be an way to do this.

Therefor there must be another way to achieve this that i am not aware of, so, if anyone could enlighten me i would be most appreciative!

Thanks!

Upvotes: 5

Views: 5048

Answers (3)

ICL1901
ICL1901

Reputation: 7778

for Swift 3, and based on Caleb's answer, this works for me:

cell.wantsLayer = true
cell.layer?.backgroundColor = NSColor.red.cgColor 

It took me ages to find this question and answer, and I'm most appreciative.

Upvotes: 1

Caleb
Caleb

Reputation: 124997

A NSTableCellView is a NSView, and a NSView has a CALayer, and a CALayer has a backgroundColor. So...

myTableCellView.wantsLayer = YES;  // make the cell layer-backed
myTableCellView.layer.backgroundColor = [[NSColor redColor] CGColor]; // or whatever color you like

Upvotes: 12

c9s
c9s

Reputation: 1917

You need to convert your NSColor object into a CGColor struct:

self.layer.backgroundColor = [[NSColor redColor] CGColor];

Upvotes: 1

Related Questions