macosnstableviewnsvisualeffectview

NSTablewView, Highlight Mode "None" and Blurred Background


I have noticed that when my view-based NSTableView has the highlight attribute set to "Source List" in Interface Builder, it renders its background with the vibrance effect (i.e., it blurs what lies behind its parent window).

If instead I you set said attribute to "None" or "Regular" (the other two options), the background color attribute is automatically set to white, and at runtime its rendered opaque (even if a custom color with an alpha of less than 1.0 is specified).

I need my table cells to be non-highlightable, but I would really like the blur effect.

Also, I can't afford disallowing cell highlighting via the NSTableViewDelegate method:

func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool

...because that makes the text fields inside my cells unselectable and thus uneditable.

I found this question, which basically asks for the opposite solution (i.e., disable vibrance when highlight is set "Source List"), so following the answers there I tried this code on the view controller that hosts my table view:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

    let appearance = NSAppearance(named: NSAppearanceNameVibrantLight)
    tableView.appearance = appearance
}

(appearance is instantiated and assigned in two separate lines to confirm it is non-nil on the debugger)

...but it doesn't change anything.

Is there any easy workaround?


Solution

  • In Interface Builder, I had to do three things:

    1. Give the scroll view that contains the table view a transparent background color.
    2. Uncheck "Draw Background" for the scroll view.
    3. Give the table view a transparent background color.

    …then I was able to see the NSVisualEffectView that I'd placed the table inside.