How to dismiss iOS 14 datePicker modal?

3.2k views Asked by At

My development team has implemented the new iOS 14 date picker in the "compact" style, which pops a modal on top of our application. It doesn't have any controls to dismiss. As a user, I can tap outside the modal, which I figured application.windows.firstMatch.tap() would simulate well. It does when I'm debugging, but when running regularly, it is also dismissing the view behind the modal. Kind of confusing as to why it would act differently, but :shrug:

I've suggested we move to the "inline" style, but may not get my way (it's a better UX in our use-case anyway). Is anyone successfully dismissing these without unintended consequences?

"Compact datePicker"

1

There are 1 answers

3
garafajon On

This worked on the iPhone.

@IBAction func dateChanged(_ datePicker: UIDatePicker) {
    model.date = datePicker.date
    self.dismiss(animated: true, completion: nil)
}

Maybe if this dimisses too much on iPad you need to set definesPresentationContext = true on your view controller?