maitree solanki maitree solanki - 3 months ago 19
Swift Question

Set text color for UIDatePicker in 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)

Answer

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() {
    super.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)
}