Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Created December 17, 2022 07:08
Show Gist options
  • Save StewartLynch/fd0608bd3992ee2d04e5552c0450375f to your computer and use it in GitHub Desktop.
Save StewartLynch/fd0608bd3992ee2d04e5552c0450375f to your computer and use it in GitHub Desktop.
import SwiftUI
enum Style: String, CaseIterable {
case grouped, inset, insetGrouped, plain, sidebar
}
struct ContentView: View {
@State private var selectedStyle: Style = .plain
var body: some View {
VStack {
Picker("List Style", selection: $selectedStyle) {
ForEach(Style.allCases, id: \.self) { style in
Text(style.rawValue)
}
}
List {
Section("Breakfast") {
Text("pancakes")
Text("bacon")
Text("orange juice")
}
Section("Lunch") {
Text("sandwich")
Text("chips")
Text("lemonade")
}
Section("Dinner") {
Text("spaghetti")
Text("bread")
Text("water")
}
}
.listStyle(for: selectedStyle)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension List {
@ViewBuilder
func listStyle(for customListStyle: Style) -> some View {
switch customListStyle {
case .grouped:
listStyle(.grouped)
case .inset:
listStyle(.plain)
case .insetGrouped:
listStyle(.insetGrouped)
case .plain:
listStyle(.plain)
case .sidebar:
listStyle(.sidebar)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment