I have this DatePicker inside a VStack, working fine:
VStack {
DatePicker(selection: $birthDate, displayedComponents: .date) {
Text("")
}.padding(10)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
}
I'd like to be able to have the default date set to 40 years in the past so the user doesn't have to spin the year so far (most people aren't newborns). This can be done with DatePicker as shown in this SO answer. I don't know how to implement that in SwiftUI. Thanks for any help.
Why not just like this?
struct ContentView: View {
@State private var birthDate: Date = Calendar.current.date(byAdding: DateComponents(year: -40), to: Date()) ?? Date()
var body: some View {
VStack {
DatePicker(selection: $birthDate, displayedComponents: .date) {
Text("")
}
.padding(10)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With