有没有办法将星期几添加到我的UIDatePicker?我想表明所选日期是星期一还是星期二等。
let picker = UIDatePicker()
func createDatePicker(){
let toolbar = UIToolbar()
toolbar.sizeToFit()
let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([done], animated: false)
dateField.inputAccessoryView = toolbar
dateField.inputView = picker
picker.datePickerMode = .date
}
@objc func donePressed(){
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
let dateString = formatter.string(from: picker.date)
dateField.text = "\(dateString)"
self.view.endEditing(true)
}
答案 0 :(得分:1)
在UIDatePicker
中显示一周中某一天的唯一方法是将datePickerMode
设置为dateAndTime
。或者,您可以使用datePicker
创建自定义UIPickerView
。但是,这将是一种麻烦的方法。