How to display DatePicker in SwiftUI?
pickers
In this post we will see how to create a simple Date Picker in SwiftUI.
Let’s create a @State
variable that will keep track of the current selected date.
We will next initialize the DatePicker and pass in the departureDate
If you want to display the time along with the date you would pass
both date
and hourAndMinute
to the displayedComponents
parameter.
If you want to perform any action after a new departure date is selected by the user
you can apply the .onChange
modifier and capture the newly selected date.
You can also change the style of the DatePicker
by applying the .datePickerStyle
modifier.
There are different styles available like .wheel
, .graphical
, .compact
and .automatic
.
Sponsored