Remove/change section header background color in SwiftUI List

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

    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!

    List {
        Section(header: HStack {
            Text("Header Text")
                .font(.headline)
                .padding()
    
                Spacer()
        }
        ) {
    ForEach(0...3) { row in
                            Text("Row")
                        }
        }
    }.listStyle(GroupedListStyle()).listSeparatorStyle()
    
    public struct ListSeparatorStyleModifier: ViewModifier {
        public func body(content: Content) -> some View {
            content.onAppear {
                UITableView.appearance().separatorStyle = .singleLine
                UITableView.appearance().separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                UITableViewHeaderFooterView.appearance().tintColor = .clear
                UITableView.appearance().backgroundColor = .clear // tableview background
                UITableViewCell.appearance().backgroundColor = .clear
    
            }
        }
    }
    
    extension View {
        public func listSeparatorStyle() -> some View {
            modifier(ListSeparatorStyleModifier())
        }
    }
    
    

提交回复
热议问题