SwiftUI DatePicker in Form Dismisses When Keyboard Appears

1.3k Views Asked by At

I have a SwiftUI Form with a Section that contains a DatePicker. The DatePicker is set to have a datePickerStyle of CompactDatePickerStyle(), by default, since it's in a Form.

Screenshot of form containing date picker

When tapping on the DatePicker, the overlay is presented:

Screenshot of date picker overlay

The DatePicker's time is able to be modified by using a gesture, as seen in the following video:

Video of modifying date picker's time via gestures

In the following video, tapping on the overlaid DatePicker's time to modify it via the keyboard causes the overlay to be dismissed:

Video of attempt to modify date picker's time via keyboard input

I have also tried adding the following to the DatePicker in order to allow for inline date manipulation, hoping for keyboard avoidance:

.datePickerStyle(GraphicalDatePickerStyle())
.ignoresSafeArea(.keyboard, edges: .bottom)

However, the above results in the following:

Video of attempt to edit date inline with keyboard avoidance

What do I need to change to allow the DatePicker to be manipulated via gestures and keyboard input within the form?

2

There are 2 best solutions below

2
On

You need to raise the datepicker so the keyboard wouldn't show over the datepicker, already reported this bug in October 2020 and the ticket is still open. These datepicker don't have keyboard avoidance. Also found an issue with GraphicalDatePickerStyle that can crash the app, ticket still open too.

0
On

Did you try to use .ignoresSafeArea(.keyboard) on the entire Form? I had the same issue and this fixed it for me.