Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created June 19, 2020 04:17
Show Gist options
  • Save laevandus/3eeaca92b8d80a8fc070d7ce41aae12b to your computer and use it in GitHub Desktop.
Save laevandus/3eeaca92b8d80a8fc070d7ce41aae12b to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State private var selectedFruit: Fruit = .orange
var body: some View {
NavigationView {
Form {
Section {
makeFruitPicker()
makePlanetPicker()
}.navigationBarTitle("Pickers")
}
}.navigationViewStyle(StackNavigationViewStyle())
}
// MARK: Picker with Segmented Style
enum Fruit: String, CaseIterable {
case apple, orange, pear
}
func makeFruitPicker() -> some View {
Picker("Fruits", selection: $selectedFruit) {
ForEach(Fruit.allCases, id: \.rawValue) { fruit in
Text(fruit.rawValue).tag(fruit)
}
}.pickerStyle(SegmentedPickerStyle())
}
// MARK: Picker with Default Style
@State private var selectedPlanet = ""
let planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Neptune", "Uranus"]
func makePlanetPicker() -> some View {
Picker("Planets", selection: $selectedPlanet) {
ForEach(planets, id: \.self) { planet in
Text(planet)
}.navigationBarTitle("Planets")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment