使用List
SwiftUI中的简单控件,如何更改/删除节标题的标准背景颜色
struct ContentView : View { var body: some View { List { ForEach(0...3) { section in Section(header: Text("Section")) { ForEach(0...3) { row in Text("Row") } } } } } }
在beta 4中,不推荐使用relativeWidth。更新了代码以反映这一点。
不幸的是,没有快速的参数可以设置背景颜色。但是,您仍然可以这样做:
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()
}
}
}