Remove/change section header background color in SwiftUI List

后端 未结 6 1272
旧巷少年郎
旧巷少年郎 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:43

    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")
                    }
                }
            }
         }
      }
    }
    

提交回复
热议问题