我的 UI DatePicker 格式不正确
My UIDatePicker Is Not Coming Propper format
这是我的代码。
extension ProfileViewController {
private func openDatePicker() {
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
ageTextFieldOutlet.inputView = datePicker
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))
let cancleButton = UIBarButtonItem(title: "Cancle", style: .plain, target: self, action: #selector(cancleButtonClicked))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButtonClicked))
let flexibleBtn = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancleButton, flexibleBtn, doneButton], animated: true)
ageTextFieldOutlet.inputAccessoryView = toolBar
}
@objc func cancleButtonClicked() {
ageTextFieldOutlet.resignFirstResponder()
}
@objc func doneButtonClicked() {
if let datePiker = ageTextFieldOutlet.inputView as? UIDatePicker {
print(datePiker.date)
}
}
}
// MARK: - UITextFieldDelegate For Age
extension ProfileViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
self.openDatePicker()
}
}
为了更好的理解,还有图片。
我在 运行 我的项目中得到了这种类型的选择器
我想要这种类型的日期选择器
在iOS (13.4+) 的较新版本中,如果您想要轮式日期选择器,则需要指定样式,默认情况下它是自动的,并且通常默认为紧凑型,在这种情况下,当您创建一个新的 UIDatePicker 你可以这样做:
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .wheels
ageTextFieldOutlet.inputView = datePicker
文档:
这是我的代码。
extension ProfileViewController {
private func openDatePicker() {
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
ageTextFieldOutlet.inputView = datePicker
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))
let cancleButton = UIBarButtonItem(title: "Cancle", style: .plain, target: self, action: #selector(cancleButtonClicked))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButtonClicked))
let flexibleBtn = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolBar.setItems([cancleButton, flexibleBtn, doneButton], animated: true)
ageTextFieldOutlet.inputAccessoryView = toolBar
}
@objc func cancleButtonClicked() {
ageTextFieldOutlet.resignFirstResponder()
}
@objc func doneButtonClicked() {
if let datePiker = ageTextFieldOutlet.inputView as? UIDatePicker {
print(datePiker.date)
}
}
}
// MARK: - UITextFieldDelegate For Age
extension ProfileViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
self.openDatePicker()
}
}
为了更好的理解,还有图片。
我在 运行 我的项目中得到了这种类型的选择器
我想要这种类型的日期选择器
在iOS (13.4+) 的较新版本中,如果您想要轮式日期选择器,则需要指定样式,默认情况下它是自动的,并且通常默认为紧凑型,在这种情况下,当您创建一个新的 UIDatePicker 你可以这样做:
let datePicker = UIDatePicker()
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .wheels
ageTextFieldOutlet.inputView = datePicker