0

I wanna remove the bakcground of the default list. All of these solution didn't work, it's there any new modifier we can use to remove bakcground color?

I‘ve tried

.scrollContentBackground(.hidden)
                .background(Color.red)

and

 UITableView.appearance().separatorStyle = .none
                UITableView.appearance().backgroundColor = .clear
                UITableViewCell.appearance().backgroundColor = .clear

The structure is like this

VStack{
            Text("My Task")
                .font(.title3).bold()
                .padding()
                .frame(maxWidth: .infinity, alignment: .leading)
            
            List{
                ForEach(realmManager.task, id: \.id){
                    TaskViewItem(task: $0.title, complete: $0.complete).listRowBackground(Color.clear)
                }
            } .scrollContentBackground(.hidden)
            
        }.frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color(hue: 0.086, saturation: 0.141, brightness: 0.972))

the TaskViewItem class


    var body: some View {
        HStack(spacing: 20){
            Image(systemName: !complete ? "circle" : "circle.fill")
            Text(task)
        }
    }
  • Show a minimal reproducible code that produces your issue, see: [minimal code](https://stackoverflow.com/help/minimal-reproducible-example). Is your `List` inside a `Form`? is it using a `ForEach` loop? these could affect the possible answers. Also have a look at these answers: https://stackoverflow.com/questions/56517904/how-do-i-modify-the-background-color-of-a-list-in-swiftui – workingdog support Ukraine Jul 12 '23 at 02:06

1 Answers1

0

Try this approach, using a combination of .scrollContentBackground(.hidden) and .listRowBackground() to remove the bakcground of a List

 struct ContentView: View {
     let names = ["January", "February", "March", "April"]
     
     var body: some View {
         ZStack {
             // for testing, yellow background
             Color.yellow.ignoresSafeArea()
             List (names, id: \.self) { name in
                 Text(name)
                     .listRowBackground(Color.clear) // <-- here
             }
              .scrollContentBackground(.hidden) // <-- here
         }
     }
 }

EDIT-1:

to show it also work with a custom view TaskViewItem(...)

 struct ContentView: View {
    let names = ["January", "February", "March", "April"]
    
    var body: some View {
        VStack {
            Text("My Task")
                .font(.title3).bold()
                .padding()
                .frame(maxWidth: .infinity, alignment: .leading)
            
            List {
                ForEach(names, id: \.self) {
                    TaskViewItem(name: $0).listRowBackground(Color.clear)
                }
            }.scrollContentBackground(.hidden)
        }
        .background(Color(hue: 0.086, saturation: 0.141, brightness: 0.972))
    }
}

struct TaskViewItem: View {
    @State var name: String
    var body: some View {
        Text(name)
    }
}

EDIT-2:

code with simulated ForEach(realmManager.task, id: \.id) { task in ...}. Note the important closure parameter task in ...

struct ContentView: View {
    let tasks = [MyTask(title: "task-1", complete: false),
                 MyTask(title: "task-2", complete: true),
                 MyTask(title: "task-3", complete: false)]
    
    var body: some View {
        VStack {
            Text("My Task")
                .font(.title3).bold()
                .padding()
                .frame(maxWidth: .infinity, alignment: .leading)
            
            List {
                ForEach(tasks, id: \.id) { task in
                    TaskViewItem(task: task.title, complete: task.complete)
                        .listRowBackground(Color.clear)
                }
            }
            .scrollContentBackground(.hidden)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.pink)  // <-- for testing
      // .background(Color(hue: 0.086, saturation: 0.141, brightness: 0.972))
    }
}

struct TaskViewItem: View {
    let task: String
    let complete: Bool
    
    var body: some View {
        HStack(spacing: 20){
            Image(systemName: !complete ? "circle" : "circle.fill")
            Text(task)
        }
    }
}

struct MyTask: Identifiable {
    let id = UUID()
    var title: String
    var complete: Bool
}