Change UIDatePicker selected Date text Color for D

2020-08-10 07:46发布

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:

enter image description here

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.

4条回答
孤傲高冷的网名
2楼-- · 2020-08-10 08:33

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
查看更多
别忘想泡老子
3楼-- · 2020-08-10 08:47

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.

查看更多
乱世女痞
4楼-- · 2020-08-10 08:48

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

查看更多
姐就是有狂的资本
5楼-- · 2020-08-10 08:49

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
}
查看更多
登录 后发表回答