I want to add a Done button within a popped up datePickerView in Swift.
Here is the code:
@IBOutlet var datePicker: UITextField!
@IBAction func dateTextInputPressed(sender: UITextField) {
var datePickerView = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.Date
sender.inputView = datePickerView
datePickerView.addTarget(self, action: Selector("handleDatePicker:"), forControlEvents: UIControlEvents.ValueChanged)
}
func handleDatePicker(sender: UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
datePicker.text = dateFormatter.stringFromDate(sender.date)
}
I can use this code to pop up a datePickerView successfully.
But after I've selected the date, it does not have a "Done" button to dismiss it.
So how can I add the Done button into it?
Thanks!
I made this extension to close the picker. Swift 2
Swift 3 - 4
And you can use it simply in your viewController:
if you are using a button instead of text field:
/** * MARK - Advance booking action */
You can use a generic
UIView
for theinputView
property of theUITextField
. We can add theUIDatePicker
andUIButton
to thisUIView
.