Nico Cobelo
Nico Cobelo

Reputation: 767

Dismissing the datePicker calendar when selecting a date

I have a problem with the new datePicker update. Whenever I select a date in the calendar, in order to dismiss it, I have to tap anywhere out of the date picker to dismiss it. Is there a way to dismiss the date picker whenever I select a date?

Upvotes: 0

Views: 1957

Answers (3)

Marick Sumanta
Marick Sumanta

Reputation: 21

@IBAction func DateSelected(_ sender: Any) {
    //Set your datePicker Value change
    self.dismiss(animated: true, completion: nil)
}

Upvotes: 0

Ozgun
Ozgun

Reputation: 42

I thought of a very funny solution. It works perfectly though!

self.datePicker.addTarget(self, action: #selector(pickerTapped), for: .primaryActionTriggered)

You select '.primaryActionTriggered' because this way your 'pickerTapped' function gets fired everytime you click on a different date in your calendar.

Then you do this:

@objc func pickerTapped() {
        self.datePicker.preferredDatePickerStyle = .wheels
        self.datePicker.preferredDatePickerStyle = .automatic
    }

Your date picker switches from .wheels to your preferred choice so fast, the human eye doesn't see it change. All the user sees is the date picker collapsing after they selected a date.

Upvotes: 2

yasin89
yasin89

Reputation: 173

I assume, you using done press button after selecting date. Just put this code end of your done press like:

func doneButtonPressed() {
 // Your code block

    self.view.endEditing(true)
 // view is your mainView
}

Upvotes: 0

Related Questions