Reputation: 61
When I select my picker I only want to show years from 1900 to 2150 What's wrong in my code or what's missing?
@objc func dateChanged(datePicker: UIDatePicker,field: HoshiTextField){
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let min = dateFormatter.date(from: "01-01-1900")
let max = dateFormatter.date(from: "01-01-2150")
datePicker.minimumDate = min
datePicker.maximumDate = max
if self.fechaTxt.isFirstResponder{
self.fechaTxt.text = dateFormatter.string(from: datePicker.date)
}
self.view!.endEditing(true)
}
Upvotes: 0
Views: 566
Reputation: 8493
When setting the min and max date of a UIDatePicker, other dates will still be visible, they just can't be selected. So if you try to scroll to the year 1800, your picker will bounce up to your minimumValue of 1900.
If you want to prevent date us beyond your time range to be shown, I'm afraid you'll have to create it yourself, by using UIPickerView, and populate it with however many components you want.
Or, as this is likely a common problem, I guess several other people have created a pod or spm just for this case, so check the internet for alternatives if you don't want to re-invent this wheel.
Upvotes: 1