With a simple List
in SwiftUI, how do I change/remove the standard background color for the section header
struct ContentView : View {
var body:
I was able to get the header to be clear (become white in my case) by using the custom modifiers. I needed to use listStyle() modifiers and all of the above didn't work for me.
Hope it helps someone else!
List {
Section(header: HStack {
Text("Header Text")
.font(.headline)
.padding()
Spacer()
}
) {
ForEach(0...3) { row in
Text("Row")
}
}
}.listStyle(GroupedListStyle()).listSeparatorStyle()
public struct ListSeparatorStyleModifier: ViewModifier {
public func body(content: Content) -> some View {
content.onAppear {
UITableView.appearance().separatorStyle = .singleLine
UITableView.appearance().separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
UITableViewHeaderFooterView.appearance().tintColor = .clear
UITableView.appearance().backgroundColor = .clear // tableview background
UITableViewCell.appearance().backgroundColor = .clear
}
}
}
extension View {
public func listSeparatorStyle() -> some View {
modifier(ListSeparatorStyleModifier())
}
}