将工作日添加到UIDatePicker

时间:2018-01-09 06:34:25

标签: ios swift 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)
}

1 个答案:

答案 0 :(得分:1)

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