-3

I have a list in a sheet I'm presenting and I can't seem to set the background color on it. I can verify it's the list itself (rather than something to do with the background color of the sheet). If I set a max width on the list I can see the correct background color surrounding the edges of the list.

My sheet view is as follows

struct WorkoutView: View {
    var body: some View {
        ZStack {
            // ...
        }
        .sheet(isPresented: $showSheet) {
            ExercisesList()
        }
    }
}

struct ExercisesList: View {
    init() {
        UITableView.appearance().backgroundColor = .clear
        UITableViewCell.appearance().backgroundColor = .clear
    }
    
    var body: some View {
        List(exercises, id: \.self) { exercise in
            HStack {
                Button {} label: {
                    HStack {
                        Text(exercise)
                            .foregroundColor(.white)
                        
                        Spacer()
                    }
                        
                }
                .padding(.horizontal)
                .padding(.vertical, 18)
            }
            .listRowInsets(
                .init(
                    top: 0,
                    leading: 0,
                    bottom: 0,
                    trailing: 0
                )
            )
            .listRowBackground(Color.black)
            .listRowSeparatorTint(Color.white.opacity(0.2))
        }
        .listStyle(.plain)
        .listRowBackground(Color.black)
        .background(Color.black.edgesIgnoringSafeArea(.all))
    }
}

I've also tried to put the List in a ZStack with a Color.black.edgesIgnoringSafeArea(.all), but since the list sits on top of the Color it's just displaying white underneath.

csm232s
  • 1,660
  • 4
  • 32
  • 60
  • Does this answer your question? [SwiftUI List color background](https://stackoverflow.com/questions/57128547/swiftui-list-color-background) – workingdog support Ukraine Jan 07 '23 at 03:34
  • No, I attempted to use that but as stated in the OP the list view sits on top of the color so you just see the white background of the list instead of the background color. – csm232s Jan 07 '23 at 16:12

1 Answers1

0

From iOS 16, you should hide the scrollContentBackground, so you can see underneath.

.scrollContentBackground(.hidden)

More for other versions described here

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278