The toolbar is higher than the date picker, is this a bug? or I do something wrong?
My code:
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
let datePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
showDatePicker()
}
func showDatePicker(){
//Formate Date
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .compact
//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([doneButton,spaceButton,cancelButton], animated: false)
textField.inputAccessoryView = toolbar
textField.inputView = datePicker
}
@objc func donedatePicker(){
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
textField.text = formatter.string(from: datePicker.date)
self.view.endEditing(true)
}
@objc func cancelDatePicker(){
self.view.endEditing(true)
}
}
Yes I know that I can go back to preferredDatePickerStyle = .wheels, but Its Interesting why does it acting like this
You can achieve your requirement from this
Objective C
if (@available(iOS 13.4, *)) {
datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
[datePicker sizeToFit];
}
In Swift
if #available(iOS 13.4, *) {
datePicker.preferredDatePickerStyle = .wheels
datePicker.sizeToFit()
}
You can try it
if #available(iOS 13.4, *) {
datePicker.preferredDatePickerStyle = .wheels
datePicker.backgroundColor = UIColor.white
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With