LeMark88 LeMark88 - 3 months ago 33
Swift Question

How to change the line color of a UIDatePicker

I have got a UIDatePicker in one of my ViewControllers. As you can see, the background is dark.

Image of my UIDatePicker

What i already managed to change is the text color to white. What i just can't change is the color of the two lines above and below the selected date. It always remains default dark grey. Does anyone have a code snipped to achieve coloring these lines?

Answer

To start with, Apple docs says, about Date Pickers, "You cannot customize the appearance of date pickers."

That being said, the following code does exactly what you need. I understand it's not the most elegant piece of code, but here it is

datePicker.subviews[0].subviews[1].backgroundColor = UIColor.redColor()
datePicker.subviews[0].subviews[2].backgroundColor = UIColor.redColor()

UIDatePicker has a subview, UIDatePickerView, which has 3 subviews, 2 of the latter of which are the two lines that indicate the selection row.