Search code examples
macosswiftuitextfield

Transparent TextField inside macOS SwiftUI list


It looks like SwiftUI has an issue on macOS where you can not set a clear background to a TextField inside a List. Is anyone aware of a workaround for this?

I have a simple project here where you can reproduce the problem.

enter image description here

I've tried both with a clear background on a TextField and by implementing my custom NSTextField with these properties (which is transparent outside a list):

    textField.drawsBackground = false
    textField.isBezeled = false 

Solution

  • I'm not sure why it's like this inside a list, but if you set drawsBackground to true and the background to .clear it works as intended, in your CustomTextField struct:

    let textField = NSTextField()
    
    textField.backgroundColor = .clear
    textField.isBezeled = false
    

    The interesting part is that the actual NSTextView inside the label doesn't seem to be exposed to view hierarchy when the field lives inside a list. It clearly imposes a strict visual style to a list field, which is .drawsBackground.