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")
}
}
}
}
}
}
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:
import SwiftUI
struct ContentView : View {
var body: some View {
List {
ForEach(0...3) { section in
Section(header: CustomeHeader(name: "Section Name", color: Color.yellow)) {
ForEach(0...3) { row in
Text("Row")
}
}
}
}
}
}
struct CustomeHeader: View {
let name: String
let color: Color
var body: some View {
VStack {
Spacer()
HStack {
Text(name)
Spacer()
}
Spacer()
}.padding(0).background(FillAll(color: color))
}
}
struct FillAll: View {
let color: Color
var body: some View {
GeometryReader { proxy in
self.color.frame(width: proxy.size.width * 1.3).fixedSize()
}
}
}
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:
public struct sectionHeader: ViewModifier{
var backgroundColor:Color
var foregroundColor:Color
public func body(content: Content) -> some View {
content
.padding(20)
.frame(width: UIScreen.main.bounds.width, height: 28,alignment:
.leading)
.background(backgroundColor)
.foregroundColor(foregroundColor)
}}
Which can be added to the sections in your list as follows:
struct ContentView: View {
@ObservedObject var service = someService()
var body: some View {
NavigationView {
List{
ForEach(someService.sections) {section in
Section(header: Text(section.title).modifier(sectionHeader(backgroundColor: Color(.systemBlue), foregroundColor: Color(.white)))){
Hope that helps somebody!
No need to change appearance of all lists or do anything strange, just:
- Put
.listStyle(GroupedListStyle())on yourList. Note: this step is essential. - Set the
listRowInsetson the section to 0. - Set the
Section.backgroundColortoclearto remove the default color, or whatever color you want to color it.
Example:
List {
Section(header: HStack {
Text("Header")
.font(.headline)
.foregroundColor(.white)
.padding()
Spacer()
}
.background(Color.blue)
.listRowInsets(EdgeInsets(
top: 0,
leading: 0,
bottom: 0,
trailing: 0))
) {
// your list items
}
}
Another way you can do it by setting the frame of the header:
VStack {
List {
ForEach(0...3) { section in
Section(header:
Text("Section")
.frame(minWidth: 0, maxWidth: .infinity,alignment:.leading)
.background(Color.blue.relativeWidth(2))
) {
ForEach(0...3) { row in
Text("Row")
}
}
}
}
}
The suggested solutions works until you decide to clear your List 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)
struct ContentView: View {
init() {
UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear
}
var body: some View {
List {
ForEach(0 ..< 3) { section in
Section(header:
Text("Section")
) {
ForEach(0 ..< 3) { row in
Text("Row")
}
}
}
}
}
}
2.With this solution you can have custom List header background color for each list in your app:
struct ContentView: View {
init() {
UITableViewHeaderFooterView.appearance().tintColor = UIColor.clear
}
var body: some View {
List {
ForEach(0 ..< 3) { section in
Section(header:
HStack {
Text("Section")
Spacer()
}
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
.background(Color.blue)
) {
ForEach(0 ..< 3) { row in
Text("Row")
}
}
}
}
}
}
来源:https://stackoverflow.com/questions/56867334/remove-change-section-header-background-color-in-swiftui-list


