Hubert Rzeminski
Hubert Rzeminski

Reputation: 469

How to make background of List cells transparent in SwiftUI?

I have this simple list that displays custom views. I can't figure out how to change the colour of the background of the cells.

List(weeksTasks, id: \.self){ week in
    let index = weeksTasks.firstIndex(of: week)
    WeekPlanRowView(hours: weekHours![index ?? 0], title: week, complete: false)
        .padding(.bottom, 15)
}

This is what I have so far: enter image description here

I would like to change the white to green, any suggestions?

I have tried this:

init(weekNum: Int, weeksTasks: [String], weekDescription: String, hoursPerWeek: Int){
        UITableView.appearance().backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.96, alpha: 0)
        UITableViewCell.appearance().backgroundColor = UIColor(red: 0.96, green: 0.96, blue: 0.96, alpha: 0)
        UITableView.appearance().tableFooterView = UIView()
}

and .listRowBackground(Color.green)

Upvotes: 1

Views: 756

Answers (1)

Asperi
Asperi

Reputation: 257749

Try the following - use ForEach inside List and apply

List {
  ForEach(weeksTasks, id: \.self){ week in
    let index = weeksTasks.firstIndex(of: week)
    WeekPlanRowView(hours: weekHours![index ?? 0], title: week, complete: false)
        .padding(.bottom, 15)
  }
  .listRowBackground(Color.green)      // << here !!
}

Tested with Xcode 12.4 / iOS 14.4

demo

Upvotes: 2

Related Questions