Description
I have UIDatePicker with datePickerMode set to date, minimumDate and maximumDate are also set. Dates that are after maximumDate are disabled and with different color to indicate that they cannot be selected.
Code for UIDatePicker
let datePicker = UIDatePicker()
datePicker.minimumDate = Date.init(timeIntervalSince1970: 0)
datePicker.maximumDate = Date()
datePicker.setValue(UIColor.yellow, forKeyPath: "textColor")
datePicker.datePickerMode = .date
Disabled future dates
When year is changed to year in the past it's expected that month and date are refreshed and color is changed, which is not the case.
Notice how 2018 has yellow color indicating that it can be selected but August and 26 do not have that color like they are not selectable. But when I scroll to them they will change color to yellow since year is 2017.
Question
How to refresh disabled date picker components when date picker changes value?
The only solution I found is switching datePickerMode to another and switching it back. It works but breaks picker animations :/
#IBAction private func datePickerChangedAction(_ sender: Any) {
self.datePicker.datePickerMode = .dateAndTime
self.datePicker.datePickerMode = .date
// ...
}
Related
Is there any way to change the language/locale of the label with "Time" string on UIDatePicker? This is my date picker configuration:
datePicker.locale = Locale(identifier: "de_DE")
datePicker.calendar.locale = Locale(identifier: "de_DE")
datePicker.datePickerMode = .dateAndTime
datePicker.preferredDatePickerStyle = .inline
And it is displayed as on the screenshot:
As you can see the "Time" label is still in English but I expected it to be in German. Is there any way to fix that or maybe at least hide this label?
I have implemented date picker in swift. I want to enable the date for next 7 day in calendar.How its is Possible.
You can set a maximum date within a UIDatePicker like this:
var datePicker = UIDatePicker()
datePicker.datePickerMode = .date
// set the start date to today if you don't want to allow dates in the past
datePicker.minimumDate = Date()
// calculate +7 days
let next7Days = Calendar.current.date(byAdding: .day, value: 7, to: Date())
//set maximum date
datePicker.maximumDate = next7Days
this will only allow to chose a date within the next 7 days
I develop my own app and I work on sign up screen.The person who sign up my app have to enter a birth date in my textfield and only user has 18-55 age accept.My textField has UIDatePicker.When datePicker open ,I want to show min and max date range.For example today now 12/02/2022 , datePicker have to show min date 12/02/2004 (user has 18) and show max date 1/1/1967 (user has 55).
https://imgur.com/a/DBpT5eq
first change the version checking line to
if #available(iOS 13.4,*) {
}
secondly use calendar for min max date
let datePicker = UIDatePicker()
if let eighteenAgo = Calendar.current.date(byAdding: .year, value: -18, to: Date()),
let afterFiftyFive = Calendar.current.date(byAdding: .year, value: 55, to: Date()) {
print(eighteenAgo.formatted(), ": Min")
print(afterFiftyFive.formatted(), ": Max")
datePicker.minimumDate = eighteenAgo
datePicker.maximumDate = afterFiftyFive
}
I am working in UIDatePicker iOS 14 with below config:
datePicker.datePickerMode = .dateAndTime
datePicker.preferredDatePickerStyle = .inline
datePicker.minimumDate = Date() // 29 Dec 2020 at 14:00
When I change the time value and the value smaller than the minimum date (Ex: 29 Dec 2020 at 12:00), the DatePicker updated UI but the value still keeps the minimum date. It's working normally in iOS 13: the date picker returns the minimum value when the change value is less than the minimum value.
Is this the bug of UIDatePicker in iOS?
Hi I am trying to change the textcolor for the UIDatePicker if preferredDatePickerStyle is UIDatePickerStyle.inline its not working, for UIDatePickerStyle.wheels and UIDatePickerStyle.compact its working
timePicker.setValue(UIColor.white, forKey: "textColor")
timePicker.preferredDatePickerStyle = UIDatePickerStyle.inline