我已经在警报中创建了时间选择器,并且在正常操作中我想向文本框添加选定的时间,是否可以从时间选择器中隐藏AM,PM,如果可能的话如何?我只需要在时间选择器中显示没有AM,PM的小时和分钟。请帮助我输入代码。
这是我的代码:
func timePicker(){
let vc = UIViewController()
vc.preferredContentSize = CGSize(width: 200,height: 200)
let timePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let dateFormatter = DateFormatter()
//dateFormatter.dateFormat = "HH:mm"
timePicker.datePickerMode = .time
vc.view.addSubview(timePicker)
let addTimePickerAlert = UIAlertController(title: "", message: "", preferredStyle: UIAlertController.Style.alert)
addTimePickerAlert.setValue(vc, forKey: "contentViewController")
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
self.hoursMinutesTextField.text = dateFormatter.string(from: timePicker.date)
print("time textfield \(self.hoursMinutesTextField.text)")
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: {
(action : UIAlertAction!) -> Void in
})
addTimePickerAlert.addAction(ok)
addTimePickerAlert.addAction(cancelAction)
self.present(addTimePickerAlert, animated: true)
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
{
self.timePicker()
return true
}
请帮助我输入代码