Is it any way to change text color of datepicker in iOS8? I've that it isn't possible in iOS7 and prior, something changed in 8th version?
For example I've found modified picker in Yahoo Weather!
Is it any way to change text color of datepicker in iOS8? I've that it isn't possible in iOS7 and prior, something changed in 8th version?
For example I've found modified picker in Yahoo Weather!
Still Works, Swift 4 Update
datePicker.setValue(UIColor.white, forKeyPath: "textColor")
This works for me.
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
It's working on iOS 10 only if you set both properties.
datePicker.setValue(UIColor.white, forKeyPath: "textColor")
datePicker.setValue(false, forKeyPath: "highlightsToday")
If you don't want the selected text to be black at start you need to set both textColor and highlightsToday :
Found solution at comments of stackoverflow. If you need just to change text color to yours and assign this subclass you your picker. For whiteColor works as magic.
Only minus i've found that color two lines of selected number is still gray.
class ColoredDatePicker: UIDatePicker {
var changed = false
override func addSubview(view: UIView) {
if !changed {
changed = true
self.setValue(UIColor.whiteColor(), forKey: "textColor")
}
super.addSubview(view)
}
}
This worked for me:
setting text color for all date picker subviews
for view in datePicker.subviews {
view.setValue(UIColor.white, forKeyPath: "textColor")
}
Possible duplicate: Set text color and font for UIDatePicker in iOS8/Swift
Resolving the issue with black "Today" via highlightsToday set to "false" has a side-effect - it's no longer has "Today" label in the list.
I've found a different solution here which works perfect without any side-effects: Set text color and font for UIDatePicker in iOS8/Swift
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
Looks like a dirty hack but that's we have at the moment: changing datePickerMode re-draws it correctly. So just set it to something different from your desired mode and change it back to original.
I found a way that wasnt listed here or in the other thread. https://stackoverflow.com/a/59838215/4266735
I ran into a similar issue with the latest SwiftUI / Swift 5 on XCode 11. All of the options above did not work and the DatePicker either stayed black text or crashed.
In your SwiftUI file set init() before var body
init() {
UIDatePicker.appearance().backgroundColor = .clear
}
Then in your var body view do this
DatePicker(selection: $dob, in: ...Date(), displayedComponents: .date) {
Text("Select Date")
}.colorInvert()
That inverted the black text to be white using the iOS Dark Theme. Looks/works great. Hope this helps.