Read complete story

Add Date and Time Picker View in UITextField (Swift)

Date & Time picker View

Some times we need to pick date or time in application for this we have to create the separate   UIView and add UIDatePicker then perform functionality on it. swift provide default feature in UITextField where we need to add view as UIDatepicker so when we tap on it , this will show the respective data whatever you have set on UIDatepicker mode.

There is Various mode of UIDatePicker

a: date picker , time picker, date & Time picker.

For doing this , you have to follow this steps.

1: take a UITextfield in UIViewcontroller and create their outlet like

@IBOutlet weak var sampleDateTextField: UITextField!

2: Now Create the outlet as ActionMode and set editing method as didbeginedit ( this will work when you just tap on UItextfield)

like as

@IBAction func specialDateTextFieldClick(_ sender: UITextField) {}

3: Now define the method

4: Here I have set action , it will call  this static method ”

Here  “ProductAttributeField” is class and name of method is  “datePickerFromValueChanged

Note: you can set date picker mode according to your requirement.

5: Now it time to define their method like that.

Here I have set formatter ,it is required to display the date or you can set different formatter .

6: Now you tap on UITextField then datepicker will show and on changing then automatic fill the date on UItextField.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

. . .

Comments (2)

Add Your Comment

  • trapti dubey
    after setting time time picker is not hiding itself ? as I wrote time.is hidden = true what is the solution
    • kunal prasad
      just call this after filling the time ” view.endEditing(true)”
      it will remove the keyboard.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close