Changing text color of datepicker

35,484

Solution 1

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)
    }
}

Solution 2

Still Works, Swift 4 Update

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

This works for me.

datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")

Solution 3

It's working on iOS 10 only if you set both properties.

datePicker.setValue(UIColor.white, forKeyPath: "textColor")
datePicker.setValue(false, forKeyPath: "highlightsToday")

Solution 4

I've changed text color through inspector (see below)

enter image description here

Solution 5

If you don't want the selected text to be black at start you need to set both textColor and highlightsToday :

enter image description here

Share:
35,484
Vasily
Author by

Vasily

Updated on February 22, 2020

Comments

  • Vasily
    Vasily over 4 years

    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!

    enter image description here

  • Syed Asad Ali
    Syed Asad Ali almost 9 years
    [datePicker setValue:[UIColor whiteColor] forKeyPath:@"textColor"]; - in Objective-C
  • Pavan Kotesh
    Pavan Kotesh over 8 years
    I did this but the current date is still Black color. Any idea what can I do instead?
  • Greg
    Greg over 8 years
    I had the same issue... it changes all the other dates except the current one.
  • clearlight
    clearlight over 8 years
    The "only minus"? Anything that doesn't look perfect is a huge minus for discerning apps and customers. It isn't adequately supported by Apple yet.
  • swiftyboi
    swiftyboi over 8 years
    Anybody find a fix for current date being black?
  • 365SplendidSuns
    365SplendidSuns over 8 years
    Working fine for me with. No problems with the current date being black.
  • Vladislav
    Vladislav almost 7 years
    Some dates are blacked
  • IvanPavliuk
    IvanPavliuk over 4 years
    'NSUnknownKeyException', reason: '[<_UIAppearance 0x280a0b0f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key textColor.'
  • Michał Ziobro
    Michał Ziobro over 4 years
    Maybe using .colorScheme(.light) will be better then inverting colours
  • אורי orihpt
    אורי orihpt over 3 years
    The second line gives me an error: Thread 1: "-[_UIDatePickerCalendarView setHighlightsToday:]: unrecognized selector sent to instance 0x7b50000c3800"
  • אורי orihpt
    אורי orihpt over 3 years
    Not working with .inline date picker style