Remove/change section header background color in SwiftUI List

后端 未结 6 1276
旧巷少年郎
旧巷少年郎 2021-02-12 12:56

With a simple List in SwiftUI, how do I change/remove the standard background color for the section header

struct ContentView : View {
    var body:         


        
6条回答
  •  不思量自难忘°
    2021-02-12 13:33

    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:
                                CustomHeader(
                                    name: "Section Name",
                                    color: Color.yellow
                                )
                            ) {
                        ForEach(0...3) { row in
                            Text("Row")
                        }
                    }
                }
            }
        }
    }
    
    struct CustomHeader: 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()
            }
        }
    }
    

提交回复
热议问题