[SOLVED] is there is a way to show old date and time picker in swift

Issue

I am new in swift and I am facing problem with new date picker
I need to show old date piker

enter image description here

But the current date piker is like this

enter image description here

Is there is any way to show old picker

My code is like this

func showDatePickerFrom(){
        //Formate Date
        let date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let result = formatter.string(from: date)
        let dateToday = formatter.date(from: result)
        
        FromdatePicker.datePickerMode = .date
        FromdatePicker.maximumDate = dateToday
        //ToolBar
        let toolbar = UIToolbar();
        toolbar.sizeToFit()
        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(donedatePicker))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelDatePicker))
        toolbar.setItems([cancelButton,spaceButton,doneButton], animated: false)
        txtdateselect.inputAccessoryView = toolbar
        txtdateselect.inputView = FromdatePicker
        
    }
    
    @objc func donedatePicker(){
        let formatter = DateFormatter()
        formatter.dateFormat = "dd-MM-yyyy"
        txtdateselect.text = formatter.string(from: FromdatePicker.date)
        self.view.endEditing(true)   
    }
    
    @objc func cancelDatePicker(){
        self.view.endEditing(true)
    }
    

Solution

From iOS 13.4, Apple change the way it show picker.

To show date picker in old way, just add below lines in the code.

if #available(iOS 13.4, *) {
    self.preferredDatePickerStyle = .wheels
}

Note : You have to write this line right after you initialize date picker.

E.x. If you write this line after setting maximum or minimum date, it won’t work like below.

if #available(iOS 13.4, *) {
    self.preferredDatePickerStyle = .wheels
}

self.minimumDate = Date()

Answered By – Fahim Parkar

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *