Beyond buttons – using advanced pickers
In this recipe, we will learn how to implement pickers—namely, Picker
, Toggle
, Slider
, Stepper
, and DatePicker
. Pickers are typically used to prompt the user to select from a set of mutually exclusive values. Toggle
views are used to switch between on/off states. Slider
views are used for selecting a value from a bounded linear range of values. As with Slider
views, Stepper
views also provide a UI for selecting from a range of values. However, steppers use the +
and –
signs to allow users to increment the desired value by a certain amount. Finally, DatePicker
views are used for selecting dates.
Getting ready
Create a new SwiftUI project named PickersApp
.
How to do it…
Let's create a SwiftUI project that implements various pickers. Each picker will have a @State
variable to hold the current value of the picker. The steps are given here:
- In the
ContentView.swift
file, create@State
variables...