swiftlistmacosswiftui

SwiftUI: how to make List view transparent? How to change List view background color?


in case of create the List with Swift UI I have uncontrollable background of the List

enter image description here

Looks like there is absent "native" way to change background color of List background color.

How to make it transparent?

(Answer is below)


Solution

  • the following code makes ALL OF Lists background color transparent:

    // Removes background from List in SwiftUI
    extension NSTableView {
        open override func viewDidMoveToWindow() {
            super.viewDidMoveToWindow()
            
            backgroundColor = NSColor.clear
            enclosingScrollView!.drawsBackground = false
        }
    }
    

    the following code makes ALL OF TextEditors background color transparent:

    extension NSTextView {
        open override var frame: CGRect {
            didSet {
                backgroundColor = .clear //<<here clear
                drawsBackground = true
            }
        }
    }