Date picker year hidden in my iOS 14 devices
Problem:
I'm having date picker with date selection with inline.In iOS 14 devices it's showing only date.Year is hidden.But in iOS 15 devices it was working fine.How can i solve that?
Solution:
In top of your controller declare the dateview as like below.
var dateView:UIView!
Then in view did load add the code like below.
datetf.inputAccessoryView = toolBar
datetf.inputView = dateView
Call the below function in viewdidload to load the datepicker
// MARK: - Date picker creation
func createPicker () {
dateView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 358))
//Add DatePicker to view
dateView.addSubview(datePicker)
toolBar.sizeToFit()
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(doneAction))
let cancelBtn = UIBarButtonItem(barButtonSystemItem: .cancel, target: nil, action: #selector(cancelAction))
let spaceBtn = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action:nil)
toolBar.setItems([doneBtn,spaceBtn,cancelBtn], animated: true)
datePicker.datePickerMode = .date
datePicker.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 14.0, *) {
datePicker.preferredDatePickerStyle = .inline
} else {
// Fallback on earlier versions
}
formatter.dateStyle = .medium
formatter.dateFormat = "MM/dd/yyyy"
}
}
Comments
Post a Comment