Having trouble to find out a solution for changing the color of text for the selected Date in iOS 13 dark mode.
I know how to change the color of the text of a UIPicker view using code
self.timePicker.setValue(UIColor.black, forKeyPath: "textColor")
OR using User Defined Runtime Attributes. But nothing is working for changing the selected date color for iOS 13 Dark mode. With background white color and black text color, my date picker-view looks like this:
So with changing the text color black, does not change the selected date text color. It changes all the other text color to black; but not the selected one. Selected one stays white, which is default for dark mode.
You can set self.window
inside AppDelegate.m
and override Interface style like so
if (@available(iOS 13, *)) {
self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
return YES;
Swift 5.*
if #available(iOS 13.0, *) {
self.window?.overrideUserInterfaceStyle = .light
}
I found something that solves the problem in a way. If I mark 'hightlightsToday' color to false, then the selected text shows with the color you set your code.
self.datePicker.setValue(false, forKey: "highlightsToday")
But if I wanted to highlight my selected date text color with a different color; in that case not sure which key value I have to change. So I am going to leave the question open, incase anyone knows how to change the selected date text color for dark mode.
You can resolve this problem via Info.plist file. Add "UIUserInterfaceStyle" key and "Light" value in Info.plist file.
Reference: https://github.com/xgfe/react-native-datepicker/issues/365#issuecomment-532875809
You can write an extension for UIDatePicker and then set the textColor of the datePicker text to UIColor.label, it will dynamically change according to the interface mode(light/dark mode).
extension UIDatePicker {
var textColor: UIColor? {
set {
setValue(newValue, forKeyPath: "textColor")
}
get {
return value(forKeyPath: "textColor") as? UIColor
}
}
var highlightsToday : Bool? {
set {
setValue(newValue, forKeyPath: "highlightsToday")
}
get {
return value(forKey: "highlightsToday") as? Bool
}
}
}
After this, you can set the textColor value like:-
let datePicker = UIDatePicker()
datePicker.textColor = UIColor.label