swiftswiftui

How do I modify the background color of a List in SwiftUI?


I'm trying to recreate an UI I built with UIKit in SwiftUI but I'm running into some minor issues.

I want the change the color of the List here, but no property seems to work as I expects. Sample code below:

struct ListView: View {
    @EnvironmentObject var listData: ListData

       var body: some View {
        NavigationView {
            List(listData.items) { item in
                ListItemCell(item: item)
            }
            .content.background(Color.yellow) // not sure what content is defined as here
            .background(Image("paper-3")) // this is the entire screen 
        }
    }
}

struct ListItemCell: View {
    let item: ListItem

    var body: some View {

        NavigationButton(destination: Text(item.name)) {
            Text("\(item.name) ........................................................................................................................................................................................................")
                .background(Color.red) // not the area I'm looking for
        }.background(Color.blue) // also not the area I'm looking for
    }
}

I want to change the WHITE area


Solution

  • Ok, I found the solution for coloring the list rows:

    struct TestRow: View {
    
        var body: some View {
            Text("This is a row!")
            .listRowBackground(Color.green)
        }
    }
    

    and then in body:

    List {
        TestRow()
        TestRow()
        TestRow()
    }
    

    This works as I expect, but I have yet to find out how to then remove the dividing lines between the rows...