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:
- Exit your app, go to Settings.
- Select General
- Select International
- Select the bottom option, Region Format.
- Change to the desired region.
- Relaunch your app and see the new UI Picker!
Related Topics
Cleanly Handling /Usr/Local/ with Swift Package Manager and Libevent
Why Is There Multiple Collision Calls Sprite Kit Swift
How to Custom The Image of Mkannotation Pin
Protocol Variable Implementing Another Protocol
Skphysicscontact Not Detecting Categorybitmask Collision
Verifying The Purchase (Receipt) of Another Application (Mac App Store)
Use Tableviewcontroller Inside Skscene
Hit Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Value (Lldb)
How to Populate Table Rows, Using a [String] Array Sent from iPhone by Watch Connectivity
Expose an Interface of a Class Loaded from a Framework at Runtime
Arkit: How to Tell If User's Face Is Parallel to Camera
Swift Compound Arithmetic Operation Error
How to Apply Impulse to The Node on Touch Angle
Swift: How to Get Image Name from Assets
Need Clarification on Anyobject in Swift
Firebase Data Retrieval - Edit Annotation
Swift Dictionary Initialization of Custom Type Gives: '>' Is Not a Postfix Unary Operator Error