Set background color to NSTableCellView

I am trying to set the background color in an NSTableCellView, but there seems to be no way to do this.

For this, there must be another way to achieve this, which I do not know about, therefore, if someone could enlighten me, I would be very grateful!

Thanks!

+4
source share
3 answers

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

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

You need to convert your NSColor object to a CGColor structure:

 self.layer.backgroundColor = [[NSColor redColor] CGColor]; 
+1
source

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 years to find this question and answer, and I am very grateful.

+1
source

Source: https://habr.com/ru/post/1492766/


All Articles