Adding background color for full screen containing List/Tableview in swiftUI

Viewed 62

I wanted to change background color of the whole screen(including safe area) in SwiftUI.

Adding following code to outermost view works for all the views except if given view contains List

.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)

I wanted to add background color for the full screen containing List. I have tried multiple things like,

Adding ZStack with first element as Color.red, but not worked

Color.red with List as an overlay, but it still not worked.

Only thing working for me is converting List to ScrollView, but that I don't want.

Do anyone have any other solution to make that work, or Apple doesn't provide anyway to change background color for List?(should support atleast from iOS14 to latest)

1 Answers

Update: Xcode 14b4 - new beta, new fun.

There is new modifier on Xcode 14 for that (Xcode 14b4):

enter image description here

var body: some View {
    List {
        Text("Item 1")
        Text("Item 2")
        Text("Item 3")
    }
    //.scrollContentBackground(Color.blue) // << absent now !!
    .scrollContentBackground(.hidden) // Xcode 14b4+
    .background(Color.blue) // << also this needed !!
}
Related