3

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 :)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Daniel Arantes Loverde
  • 2,317
  • 2
  • 28
  • 41
  • 2
    Never dig into the private subview structure of standard iOS controls. That is private and could change in any iOS update. Only use the public API to do what you need. If there is no public API, then you can't make the changes you want. – rmaddy Sep 20 '17 at 03:33
  • 4
    The only valid solution is to create your own date picker using `UIPickerView`. It's not trivial but it's the only way to get the exact results that you want. – rmaddy Sep 20 '17 at 03:34

1 Answers1

1

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.

Pratik Patel
  • 439
  • 5
  • 15
  • Seams wired, because the PickerView is not DatePicker, and, how to get a string month, day and year ? It is generated automatically. Not sure it is right solution to change text color – Daniel Arantes Loverde Sep 20 '17 at 01:39