Note : When user click on UITextfield, UIDatePicker is open.
step 1: Put UITextfield and give their IBOutlet and textfielddelegate.
step 2 : In ViewController, create variable of UIDatepicker. Like,
step 1: Put UITextfield and give their IBOutlet and textfielddelegate.
step 2 : In ViewController, create variable of UIDatepicker. Like,
var date_1 = UIDatePicker()
step 3: Create function with argument. Like,
func pick_Date(_ textfield : UITextField) {
//For create date picker...
self.date_1 = UIDatePicker(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 215))
self.date_1.backgroundColor = UIColor.white
lf.date_1.datePickerMode = .date
self.txt_select_date.inputView = date_1
//for create toolbar...
let toolbar = UIToolbar()
toolbar.barStyle = .default
toolbar.isTranslucent = true
toolbar.tintColor = UIColor(red: 90/255, green: 215/255, blue: 255/255, alpha: 1)
toolbar.sizeToFit()
//Add button toolbar...
let btn_add = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(Date_ViewController.click_Done))
let btn_space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let btn_cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(Date_ViewController.click_Cancel))
toolbar.setItems([btn_add, btn_space, btn_cancel], animated: true)
toolbar.isUserInteractionEnabled = true
txt_select_date.inputAccessoryView = toolbar
}
step 4: Create done button and cancel button item function. Like,
func click_Done() {
let date_Formatter = DateFormatter()
date_Formatter.dateStyle = .medium
date_Formatter.timeStyle = .none
txt_select_date.text = date_Formatter.string(from: date_1.date)
txt_select_date.resignFirstResponder()
}
func click_Cancel() {
txt_select_date.resignFirstResponder()
}
step 5: Call "pic_date" function on textfieldDidBeginEditing delegate method. Like,
func textFieldDidBeginEditing(_ textField: UITextField) {
self.pick_Date(self.txt_select_date)
}
Thank you...
No comments:
Post a Comment