将工作日添加到 UIDatePicker

Add weekday to UIDatePicker

有没有办法将星期几添加到我的 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)
}

UIDatePicker 中显示星期几的唯一方法是将 datePickerMode 设置为 dateAndTime。或者,您可以使用 UIPickerView 创建自定义 datePicker。但是,这将是一种麻烦的方法。