0

I am trying to change the text color for UIDatePicker using setValue() function, but I don't see any change on the UI. I am displaying date picker on a UITableViewCell and testing it on iOS 15 device.

How can I change the text color for date picker? I also want to change highlight color for Today, but my app is crashing when I uncomment the setValue() line for "highlightsToday" in below code.

Below is my code:

func formatCell(checkInDate: Date) {
    dateTimePicker.setValue(UIColor.red, forKeyPath: "textColor")
    // dateTimePicker.setValue(false, forKeyPath: "highlightsToday")
    dateTimePicker.addTarget(self, action: #selector(handler(_:)), for: UIControl.Event.valueChanged)
    dateTimePicker.date = checkInDate
    dateTimePicker.minuteInterval = 30
    dateTimePicker.datePickerMode = .dateAndTime
    dateTimePicker.minimumDate = Date().nearest30Mins()
    if #available(iOS 13.4, *) {
        dateTimePicker.preferredDatePickerStyle = .compact
    }
 }
tech_human
  • 6,592
  • 16
  • 65
  • 107
  • The question of how to change the text color of a date picker has been explored in other questions here on Stack Overflow (e.g. https://stackoverflow.com/questions/69595396/swift-problem-change-text-color-in-datepicker-format-compact#comment123032739_69595396). It's increasingly looking like there is no way to customize the text colors of the system date picker. – Scott Thompson Oct 20 '21 at 20:51
  • @tech_human There is no public API to do such a thing. If you want to call private API, go ahead explore the runtime headers of UIDatePicker. https://developer.limneos.net/index.php?ios=11.0&framework=UIKit.framework&header=UIDatePicker.h – RTXGamer Oct 20 '21 at 20:55

0 Answers0