4

Using iOS14.0.1, Swift5.3, Xcode12.0.1,

I try to change the textColor of a UIDatePicker in iOS14 and using UIKit.

I read here that you need the following method to do so:

let myDatePicker = UIDatePicker()
myDatePicker.tintColor = .white

I also tried the following (but that makes my app crash under iOS14):

myDatePicker.setValue(UIColor.white, forKeyPath: "textColor")

I also tried (but without success either):

UILabel.appearance(whenContainedInInstancesOf: [UIDatePicker.self]).textColor = UIColor.white

In light mode, none of my trials work (as can bee seen in the screenshot-excerpt):

enter image description here

What do I need to do to get the white color text in my UIDatePicker?

egeeke
  • 683
  • 1
  • 4
  • 16
iKK
  • 5,557
  • 6
  • 51
  • 111
  • If you have a use case for this feature, please file an enhancement request with Apple. – matt Oct 07 '20 at 15:24

3 Answers3

4

Try this, add the extension below:

extension UIDatePicker {

var textColor: UIColor? {
    set {
        setValue(newValue, forKeyPath: "textColor")
    }
    get {
        return value(forKeyPath: "textColor") as? UIColor
    }
  }
}

Now in viewDidLoad call:

myDatePicker.textColor = .yourColor

call it after other picker property, es.:

myDatePicker.preferredDatePickerStyle = .wheels
myDatePicker.addTarget(self, action: #selector(handleDatePicker), for: .valueChanged)
myDatePicker.datePickerMode = .dateAndTime
myDatePicker.timeZone = NSTimeZone.local
myDatePicker.backgroundColor = .black
myDatePicker.setValue(false, forKey: "highlightsToday")
myDatePicker.textColor = .yourColor // here

enter image description here

if you want highlightsToday set the relative picker value to true:

myDatePicker.setValue(true, forKey: "highlightsToday")

and this is the result:

enter image description here

Fabio
  • 4,374
  • 3
  • 19
  • 21
  • 2
    Thank you Fabio - I did not have time to test your solution yet. Are you sure it will work in iOS14 since `forKeyPath: "textColor"` does not seem to work anymore in iOS14. Or is it still possible with your own extension implementation ? – iKK Oct 08 '20 at 13:12
  • 1
    @iKK Hi, I run it on Xcode 12 - iOS 14... And it works like a charme – Fabio Oct 08 '20 at 13:26
  • in my case, it doesn't work in iOS 14. Did anybody find a solution? myDatePicker.setValue(true, forKey: "highlightsToday") crashes in iOS 14 – Raspberry Dec 12 '20 at 17:39
  • crashing on ios 14.0 – Aiyub Munshi Jan 07 '21 at 12:24
  • @AiyubMunshi simply call value key after you declare myDatePicker background color... No crash now, I update my answer, thanks for the alert!!! – Fabio May 24 '21 at 08:05
  • What I was looking for `myDatePicker.setValue(false, forKey: "highlightsToday")` – Abhishek Mitra Aug 02 '21 at 12:28
3

Yes, It works! However, the proprety preferredDatePickerStyle = .wheels must be set before textColor = .yourColor, or the propetty textColor may never be affected.

0

I change picker view color using pickerView delegate method. You can add this function with add these delegate methods: UIPickerViewDelegate, UIPickerViewDataSource

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

      // change your picker view textColor etc. in here..
}
mehmett
  • 111
  • 11