Step by Step to Config UIDatePicker on your ViewController - SWIFT
Initialize UIDatePicker on your ViewController.
let toDatePicker = UIDatePicker()
Add Input view and Target for UITextField.
func createDatePicker() {
self.toDatePicker.datePickerMode = .date // Change Picker type as you want
self.toDatePicker.addTarget(self, action: #selector(toDatePickerChanged), for: .valueChanged) // Detect changes on DatePicker
self.toDatePicker.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.toDate.inputView = self.toDatePicker // Connect TextField Input view
// Date Validate //
let minDate: Date = Date().yearsBack(years: 100)
let maxDate: Date = Date().yearsBack(years: 15)
self.toDatePicker.minimumDate = minDate
self.toDatePicker.maximumDate = maxDate
}
Add Selector for DatePicker .valueChange
@objc func toDatePickerChanged() {
// Add your logic when value changes in UIDatePicker.
let formatter = DateFormatter()
formatter.dateFormat = "dd MMMM yyyy" //MMM dd,yyyy
self.toDate.text = formatter.string(from: self.toDatePicker.date)
formatter.dateFormat = "yyyy-MM-dd" //MMM dd,yyyy
// Save data
}
Add inputAccessoryView
to the TextField.
func addToolBar() {
let toolbarToDate = UIToolbar()
toolbarToDate.sizeToFit()
let doneToDateButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneDateSelect))
let spaceButton2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
toolbarToDate.setItems([spaceButton2, doneToDateButton], animated: false)
self.toDate.inputAccessoryView = toolbarToDate
}
Add Selector for Done Button.
@objc func doneDateSelect() {
self.view.endEditing(true)
}
Add addToolBar()
and createDatePicker()
to viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
self.createDatePicker()
self.addToolBar()
}