简体   繁体   中英

How to achieve the custom picker with Month, Date and time in iOS swift?

I am developing an app which requires to select the date from a picker view. But the picker view requires the Month,Date and time in the Picker view rows like in the attached image:

期望的输出

currently i am having the following code:

class ViewController: UIViewController {

   @IBOutlet weak var txtDatePicker: UITextField!
  let datePicker = UIDatePicker()

  override func viewDidLoad() {
    super.viewDidLoad()
     showDatePicker()
  }


  func showDatePicker(){
    //Formate Date
    datePicker.datePickerMode = .date

   //ToolBar
   let toolbar = UIToolbar();
   toolbar.sizeToFit()
   let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker));
   let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
  let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker));

 toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

  txtDatePicker.inputAccessoryView = toolbar
  txtDatePicker.inputView = datePicker

 }

  @objc func donedatePicker(){

   let formatter = DateFormatter()
   formatter.dateFormat = "dd/MM/yyyy"
   txtDatePicker.text = formatter.string(from: datePicker.date)
   self.view.endEditing(true)
 }

 @objc func cancelDatePicker(){
    self.view.endEditing(true)
  }
}

But with this i am getting the output as:

输出 PickerView

How to add time to the UIPickerView? Please suggest and help

Go to storyboard select your date picker view and in attribute inspector select date and time instead of Date.

And then you can select the date and time for the date picker.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM