1

This is probably an old issue. but I still couldn't find a workable solution.

Like the image below. I want to have the blue background extended to the whole list view on top. However, SwiftUI automatically adds two white backgrounds(potential from UICollection View or somewhere). Does anyone knows how to remove those white backgrounds from the SwiftUI's List?

enter image description here

enter image description here

Minimal Code to reproduce the issue. Here the two backgrounds are black and blocks out the blue background.

struct ContentView: View {
    var body: some View {
        List {
            Text("Hello World")
            Text("Hello World")
            Text("Hello World")
        }
        .background(Color.blue)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

enter image description here

Legolas Wang
  • 1,951
  • 1
  • 13
  • 26
  • You need to [edit] your question to include all relevant code as text, using proper code formatting - and not as a screenshot -, in the form of a [mcve] in order to make the question on-topic. – Dávid Pásztor Oct 18 '22 at 16:44

1 Answers1

2

On iOS 16 you need to use .scrollContentBackground(.hidden) in addition to setting background color:

List {
    Text("Hello World")
    Text("Hello World")
    Text("Hello World")
}
.background(Color.blue)
.scrollContentBackground(.hidden)

Prior to iOS 16, there was a few workarounds, the most reliable one was with retrospect (example)

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
  • 1
    Awesome, I totally missed that API, thank you so much! Together with .listRowBackground(Color.clear) I got a totally transparent List~~ – Legolas Wang Oct 18 '22 at 17:00
  • Hey bro, I have a problem and want to ask you. When the list is empty, the background will turn white or black. I can't got a transparent List!! HELP! – DoubleShy0N Nov 25 '22 at 14:08
  • @DoubleShy0N well, comments are not the best way to start a new question. Post your question(with code to reproduce the problem) instead, and someone will respond. – timbre timbre Nov 25 '22 at 20:42