Generated with AI

Member-only story

Pickers in SwiftUI

Nicola De Filippo
3 min readAug 12, 2024

In this post, I’ll present a list of the principal pickers in SwiftUI:

  • Default Picker
  • Date Picker
  • Color Picker
  • Segmented Controller (yes, it’s a picker)
  • Wheel

Default Picker

The code:

struct ContentView: View {
var films = ["A New Hope", "The Empire Strikes Back", "Return of the Jedi "]
@State private var selectedFilm = "The Empire Strikes Back"

var body: some View {
VStack {
Picker("Please choose a film", selection: $selectedFilm) {
ForEach(films, id: \.self) { film in
Text(film)
}
}
Text("You selected: \(selectedFilm)")
}
}
}

In this case, we show three film names, starting with the second one selected.

Date Picker

Now, we want to use a picker to select a date; in this case, we have more options:

struct ContentView: View {
@State private var date = Date()

var body: some View {
VStack {
DatePicker(
Nicola De Filippo
Nicola De Filippo

Written by Nicola De Filippo

Software Engineer and Entrepreneur

No responses yet

Write a response