4

I've been having some trouble trying to set the UIDatePicker font and color, but colour of label today in datePicker is not changed. Everything else in my app was fairly straightforward to adjust, except this. Does anybody know how to do this?

func updatedatePicker(){                
    DatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    DatePicker.setValue(1, forKeyPath: "alpha")    
}

In viewDidLoad() :-

DatePicker.addTarget(self, action: #selector(ViewController.updatedatePicker), forControlEvents: UIControlEvents.ValueChanged)
maitree solanki
  • 135
  • 1
  • 13

3 Answers3

7

I have face the same issue so i have try something like this, Changing the datePickerMode to something else, it will re-draw with the newly set textColor, also instead of setting textColor in updatedatePicker function set this also in viewDidLoad like this.

Answer updated to latest Swift 4

override func viewDidLoad() {
    super.viewDidLoad()
    datePicker.setValue(UIColor.white, forKeyPath: "textColor")
    datePicker.datePickerMode = .date
    datePicker.datePickerMode = .dateAndTime //Set here that mode you want.
    datePicker.addTarget(self, action: #selector(selectedDate), for: .valueChanged)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
1

If your solution didn't work you can try to add the ext color through inspector like this:

enter image description here

Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
1

To change UIDatePicker text color use:

    // MARK: - Helping content 

    private enum DatePickerProperties: String {
        case TextColor = "textColor"
        case HighlightsToday = "highlightsToday"
    }

    // MARK: - Outlets

    @IBOutlet private weak var datePicker: UIDatePicker!

    // MARK: - Lifecicle

    public override func awakeFromNib() {
        super.awakeFromNib()

        self.datePicker.setValue(UIColor.whiteColor(), forKey: DatePickerProperties.TextColor.rawValue)
        self.datePicker.setValue(false, forKey: DatePickerProperties.HighlightsToday.rawValue)
    }

It works like a charm with xCode 7.3 and Swift 2.3.

Daniel Sumara
  • 2,234
  • 20
  • 25