Dwigt
Dwigt

Reputation: 1049

Close UIDatePicker after selection when style is .compact

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.

Upvotes: 13

Views: 4105

Answers (1)

Sweeper
Sweeper

Reputation: 270860

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.

Upvotes: 14

Related Questions