Search code examples
swiftmacosnstableviewnsoutlineview

NSTableView Highlight Colour when when table lose its focus


I am using NSTableView in my project. When the user has clicked on a line, I use the standard blue selection colour. When the table lost it's focus, the selection colour changes to light grey colour.

I am trying to keep the blue selection colour even the table looses it's focus.

I've already tried overriding backgroundStyle of NSTableCellView and I can set custom colour when a cell is selected or not but the backgroundColor property is set to .light when the table lost it's focus.


Solution

  • Try to override NSTableRowView, for example I keep grey color:

    class CustomRowView: NSTableRowView {
    
    override func drawSelectionInRect(dirtyRect: NSRect) {
        NSColor.secondarySelectedControlColor().set()
        NSRectFill(dirtyRect)
    }
    }
    

    However it is a bit tricky to force NSTableView to user custom row.. If you use nibs or storyboards add new "fake" row and set your custom class. Then set identifier to be exactly "NSTableViewRowViewKey". Here is a screen shot how it should look:

    enter image description here

    This way table view (or outline view - no difference) will use your custom row view.