Is there a REAL way to change text color of selected date on UIDatePicker - Swift 3?

Viewed 925

i spend 3 days searching and testing all these methods here:

1 - Change UIDatePicker TextColor in iOS10

2 - How to change the color of UIPickerView Selector

3 - Setting the text colour of a UIDatePicker in Swift

4 - Changing text color of datepicker

Some of them, help me to change the background color of the BG line (UIview):

sender.subviews[0].subviews[0].subviews[0].subviews[2].backgroundColor = COLOR_GREEN_LIGHT

This is good, could be better when i change the date it became white and then, when stop again, became green again. But it is ok.

The real problem is to try this solution for the UILabel subview with this array solution. Because, some array positions change, and ( for example ) month name, change the position ...subviews[0] to ...subviews[1].
Not great solution for that!

As it doesn't have a Delegate to work with it. I wondering, how to REAL solution to change text color only the SELECTED DATE??

Cause, the others question satisfy all picker text, not the selected.

Thanks for any light of help :)

1 Answers

You can conform to the UIPickerViewDelegate and implement pickerView(_:attributedTitleForRow:forComponent:).

Then you can return your title like so:

return NSAttributedString(string: "YOUR STRING HERE", attributes: [NSForegroundColorAttributeName: UIColor.green])

You can also add other attributes for your string like fonts.

Related