Scubadivingfool Scubadivingfool - 1 year ago 158
Swift Question

Trying to set only time in UIDatePicker in Swift 2.0

I am trying to set the UIDatePicker to come up with the time only. The DatePicker comes up through an action when the textfield is pressed. I have not used the DatePicker Viewer. Any help is appreciated. Thanks

let starttimedatePicker = UIDatePicker()
startTimeDiveTextField.inputView = starttimedatePicker
starttimedatePicker.addTarget(self, action: "startTimeDiveChanged:", forControlEvents: .ValueChanged)

func startTimeDiveChanged(sender: UIDatePicker) {
let formatter = NSDateFormatter()
formatter.timeStyle = .ShortStyle
startTimeDiveTextField.text = formatter.stringFromDate(

Answer Source

Try setting its Mode:

starttimedatePicker.datePickerMode = UIDatePickerMode.Time
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download