With a simple List
in SwiftUI, how do I change/remove the standard background color for the section header
struct ContentView : View {
var body: some View {
List {
ForEach(0...3) { section in
Section(header: Text("Section")) {
ForEach(0...3) { row in
Text("Row")
}
}
}
}
}
}
Another way you can do it by setting the frame of the header:
In beta 4, relativeWidth was deprecated. Code updated to reflect that.
Unfortunately, there's no quick parameter to set the background color. However, you can still do it:
No need to change appearance of all lists or do anything strange, just:
.listStyle(GroupedListStyle())
on yourList
if you do not want sticky headers.listRowInsets
on the section to 0.Section.backgroundColor
toclear
to remove the default color, or whatever color you want to color it.Example:
The suggested solutions works until you decide to
clear
yourList
header background color.Better solutions for
List
header custom color:1.This solution effects all of the List sections in your app: (or move it to your
AppDelegate
class)2.With this solution you can have custom
List
header background color for each list in your app: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!
I tried to use the custom header code above, and unfortunately could not get it to work in beta 6. That led me to the use of a ViewModifier:
Which can be added to the sections in your list as follows:
Hope that helps somebody!