Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a time picker view in swift 3.0?

Tags:

ios

swift

swift3

I successfully make datePicker view over my UITextField in Swift. But I need to time picking in the format of HH:MM:SS. I have no idea about it. I need to apply picked time in time UITextField. so, How to make Time Picker in Swift? please refer below image. swift 2.2 , 2.0 answers also okay. Thanks lot friends!

enter image description here

like image 850
Rajamohan S Avatar asked Sep 18 '16 05:09

Rajamohan S


People also ask

How do I get time from date picker?

timePicker = new DateTimePicker(); timePicker. Format = DateTimePickerFormat. Time; timePicker.

What is DatePicker in Swift?

DatePicker is a control used in iOS applications to get the date and time values from the user. We can allow the user to either enter the time in point or time interval. class UIDatePicker : UIControl.


1 Answers

Updated for Swift 3:

If you want to show the time picker, use the lines of code for Swift 3 below:

Variable Declarations:

let timePicker = UIDatePicker()

Method Definition:

func openTimePicker()  {
    timePicker.datePickerMode = UIDatePickerMode.time
    timePicker.frame = CGRect(x: 0.0, y: (self.view.frame.height/2 + 60), width: self.view.frame.width, height: 150.0)
    timePicker.backgroundColor = UIColor.white
    self.view.addSubview(timePicker)
    timePicker.addTarget(self, action: #selector(yourViewControllerName.startTimeDiveChanged), for: UIControlEvents.valueChanged)
}

func startTimeDiveChanged(sender: UIDatePicker) {
    let formatter = DateFormatter()
    formatter.timeStyle = .short
    youtTextFieldName.text = formatter.string(from: sender.date)
    timePicker.removeFromSuperview() // if you want to remove time picker
}
like image 77
Kiran Jadhav Avatar answered Sep 18 '22 14:09

Kiran Jadhav