Johannes
Johannes

Reputation: 347

SwiftUI ListStyle on macOS

On macOS, using SwiftUI, regarding List:

I need to modify the List's background color.

Using the view debugger I see that the List has a ListCoresScrollView inside, with an internal _NSScrollViewContentBackgroundView and a NSVisualEffectView. That view has a white background by default. I want to change that particular view (e.g. to become transparent) because it seems to be an internal view.

How can one modify the background view on a list (in macOS, not IOS)?

Btw, I tried using a SidebarListStyle(). That does offer the background I want, but... dragging behavior is different (undesired) in a SidebarListStyle. So an alternative solution to my question could be "how to modify dragging behavior on a SidebarListStyle to mimic the default dragging behavior"?

It has taken me many many hours, but I cannot come up with a proper solution. Can anybody provide a working solution?

Upvotes: 3

Views: 1222

Answers (2)

Johannes
Johannes

Reputation: 347

I played with RichS's answer a bit more and found that the following suffices in my project:

extension NSTableView {
    open override func viewDidMoveToWindow() {
        super.viewDidMoveToWindow()

        backgroundColor = .clear
        enclosingScrollView?.drawsBackground = false
    }
}

YMMV, so please look at RichS's answer above if your scrolling does not work as expected.

Upvotes: 2

RichS
RichS

Reputation: 3147

Update #2 (about 30 mins later)...

This works :)

extension NSTableView {
    open override func viewDidMoveToWindow() {
        super.viewDidMoveToWindow()

        // set the background color of every list
        backgroundColor = .clear
        enclosingScrollView?.drawsBackground = false
        enclosingScrollView?.backgroundColor = .clear
        enclosingScrollView?.autohidesScrollers = true
        enclosingScrollView?.verticalScroller = MyScroller()
    }
}

class MyScroller: NSScroller {
    override func draw(_ dirtyRect: NSRect) {
        self.drawKnob()
    }
}

Original answer

Not sure if you are still looking for an answer, but this is the closest I've managed to get. Still can't get the scrollbars (especially background) to be actually transparent. But, is closer than I was.

Add a swift file with this in your project (for now, I've just put it at the top of my ContentView.swift file).

extension NSTableView {
    open override func viewDidMoveToWindow() {
        super.viewDidMoveToWindow()

        // set the background color of every list
        backgroundColor = .clear
        headerView = nil
        enclosingScrollView?.drawsBackground = false
        enclosingScrollView?.backgroundColor = .clear
        enclosingScrollView?.hasHorizontalScroller = false
        enclosingScrollView?.autohidesScrollers = true
    }
}

From what I've read, this should work. But, it doesn't. I perhaps don't need all of those settings, but I'll keep adding until I can get it fully working.

HTH - please let me know if you've solved it fully.

Upvotes: 4

Related Questions