Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text color for UIDatePicker in Swift

Tags:

ios

swift

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)
like image 872
maitree solanki Avatar asked Oct 19 '25 04:10

maitree solanki


2 Answers

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)
}
like image 107
Nirav D Avatar answered Oct 20 '25 19:10

Nirav D


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

enter image description here

like image 24
Tarvo Mäesepp Avatar answered Oct 20 '25 18:10

Tarvo Mäesepp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!