Automatic Select a Date in Datepicker in Swift Language

Automatic select a date in datepicker in Swift language

var dateString = "Thursday, 18 Sep 2014"
var df = NSDateFormatter()
df.dateFormat = "eeee, dd MM yyyy"
var date = df.dateFromString(dateString)
if let unwrappedDate = date {
datePicker.setDate(unwrappedDate, animated: false)
}

Swift change date picker language

You should assign loc to the date picker locale property:

let loc = Locale(identifier: "uk")
self.datePicker.locale = loc

How to pass the date picker (or just the date) to show the date on textfield in Swift?

You should simply subclass UITextField and declare your datepicker as a property of your UITextField, override didMoveToSuperview method and do the customisation there:

class DateField: UITextField {
var date: Date?
let datePicker = UIDatePicker()
let formatter = DateFormatter()
override func didMoveToSuperview() {
datePicker.datePickerMode = .date
formatter.dateStyle = .short
let toolbar = UIToolbar()
toolbar.sizeToFit()
let done = UIBarButtonItem(title: "Pronto", style: .plain, target: self, action: #selector(doneAction))
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancel = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(cancelAction))
toolbar.setItems([cancel,space,done], animated: false)
inputAccessoryView = toolbar
inputView = datePicker
}
@objc func cancelAction(_ sender: UIBarButtonItem) {
endEditing(true)
}
@objc func doneAction(_ sender: UIBarButtonItem) {
date = datePicker.date
text = formatter.string(from: datePicker.date)
endEditing(true)
}
}

UIDatePicker Click done and display text

Change your doneBtnForDate like below

func doneBtnForDate(){

if (dobTxt.text == "Please select your DOB"){
//let selectedDate = How to get picker date without scroll it?
dobTxt.text = selectedDate
}

// IF you want to change date formatting then you need to change in "DateFormatter" below
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .full
let strDate = dateFormatter.string(from: yourPicker.date)
dobTxt.text = strDate

picker.isHidden = true
toolBar2.isHidden = true
datePicker.isHidden = true
}

Select a date in UIDatePicker by UIAutomation?

you can pass your value in picker like this:

Picker.wheels()[1].selectValue(Your_Value);

in wheels()[1], [1] is your wheel number.

UIDatePicker - Problem Localizing

After further investigation, this is actually not handled by the language setting of the iPhone. The UIDatePicker is automatically localized based on the "Region Format" setting and not the language. This is odd because the dates are localized, so you would think the localization of the picker would be linked to the iDevice's language not the Region Format.

Anyway, to simulate the localized DatePicker in the Simulator:

  1. Exit your app, go to Settings.
  2. Select General
  3. Select International
  4. Select the bottom option, Region Format.
  5. Change to the desired region.
  6. Relaunch your app and see the new UI Picker!


Related Topics



Leave a reply



Submit