maitree solanki maitree solanki - 1 year ago 505
iOS Question

Set text color and font for UIDatePicker in Swift

I've been having some trouble trying to set the

font and color, but colour of
today in
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")


DatePicker.addTarget(self, action: #selector(ViewController.updatedatePicker), forControlEvents: UIControlEvents.ValueChanged)

Answer Source

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.

override func viewDidLoad() {
    datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
    datePicker.datePickerMode = .Date
    datePicker.datePickerMode = .DateAndTime //Set here that mode you want.
    datePicker.addTarget(self, action: Selector("selectedDate"), forControlEvents: .ValueChanged)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download