How do I change the cell backgroundcolor in SwiftUI and iOS 14?

Viewed 660

In my iOS app, i have SwiftUI Lists. Before iOS 14, putting this in AppDelegate worked to change the background color of the cells:

UITableViewCell.appearance().backgroundColor = .black

After iOS 14, this no longer changes the color and it has reverted to the default white.

This does not work either:

UITableViewCell.appearance().backgroundConfiguration?.backgroundColor = .black

Is there another way to change the color of a UITableViewCell in SwiftUI in iOS 14?

1 Answers

You need to use listRowBackground modifier for cells, and still can use appearance for entire table view background, like in below demo

Tested with Xcode 12 / iOS 14

demo

init() {
    UITableView.appearance().backgroundColor = UIColor.yellow
}
var body: some View {
    List {
        ForEach(0..<5, id: \.self) {
            Text("Hello \($0)").foregroundColor(.blue)
        }.listRowBackground(Color.black)
    }
}
Related