I want all the lists in my application to look exactly the same and don't want to respecify all these settings over and over. I get the following error: Cannot find type 'Configuration' in scope. I currently have something like this:
struct CustomListStyle: ListStyle {
func makeBody(configuration: Configuration) -> some View {
ScrollView {
VStack(spacing: 10) {
ForEach(configuration.data, id: \.self) { item in
Text(item as? String ?? "")
.foregroundColor(.blue)
.padding(10)
.background(Color.gray)
.cornerRadius(10)
}
}
.padding()
}
}
}
struct ContentView: View {
var body: some View {
List(0..<10, id: \.self) { index in
Text("Item \(index)")
}
.listStyle(CustomListStyle())
}
}
Edit - 1 - Here is one for reference
List( selection: $dm.someVar ) {
Section( ) {
LazyVGrid(columns: [
GridItem(.flexible(minimum: 120)),
], alignment: .leading, spacing: 10) {
ForEach(0..<20, id: \.self) { index in
Text("Column1 abcdef: \(index)")
}
}
}
}
If you try to implement a
ListStyle, you can let Xcode add stubs for protocol conformance. You will then see, that the required functions all start with_. This is a clue, that the protocolListStyleis not intended to be used for custom implementations.However, you might be able to achieve what you're after by creating a wrapper for a
Listthat accepts aViewBuilderas parameter. Something like: