11

Storyboard application using Swift.

How can I close the calendar (presented for date selection when the UIDatePicker style is .compact) after a user selects a date? Currently, the user is required to tap outside the calendar for it to close. I want it to close automatically after a user has selected a date.

I've tried resigning first responder of the UIDatePicker, ending editing in the table view controller once a notification is received that a date changed, reading another response (here) and so far nothing is working for me.

Dwigt
  • 729
  • 6
  • 16

1 Answers1

13

For now, you can do:

presentedViewController?.dismiss(animated: true, completion: nil)

i.e.

override func viewDidLoad() {
    super.viewDidLoad()
    datePicker.addTarget(self, action: #selector(dateChanged), for: .valueChanged)
}

@objc private func dateChanged() {
    presentedViewController?.dismiss(animated: true, completion: nil)
}

But note that this will potentially break in future iOS versions as this relies on the implementation detail that compact date pickers presents a view controller to show the calendar. If you print out the type of the presented VC, you'll see _UIDatePickerIOSCompactViewController. The leading _ suggests that this is an implementation detail.

All that is documented about compact is:

A style indicating that the date picker displays as a label that when tapped displays a calendar-style editor.

So in future versions of iOS, they could instead change the implementation to not present a new view controller, and this could very well break, and you'll have to find another way then. I see no documented way of doing this right now.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 3
    This is exactly what I needed and solves my problem. My specific implementation is a UITableViewController, each row in the table has a date picker, so I added the target for each datepicker in cellForRowAt. Works great. Hope it doesn't break anytime soon! – Dwigt Apr 15 '21 at 15:13
  • 6
    It is ridiculous that we need this workaround. The UIDatePicker API is embarrassing. – teradyl Oct 25 '21 at 20:20
  • 1
    Still works great for iOS 16. – Carl Smith Dec 29 '22 at 08:49