3

I had a method of changing the UIDatePicker's text color on versions of iOS before 10, but with iOS10 those solutions no longer appear to work. How could I restore the ability to change the textColor for a UIDatePicker?

   if #available(iOS 10.0, *) {
        //Not sure of a way to do something similar here.
   } else {
        //The following lines change the textColor of UIDatePicker to white in iOS9 and older.
        self.setValue(UIColor.white, forKey: "textColor")
        self.sendAction(Selector("setHighlightsToday:"), to: nil, for: nil) 
   }
AppreciateIt
  • 696
  • 2
  • 8
  • 23
  • You can go for creating custom picker using table view. It's bit complex but you can customise everything you want – Janmenjaya Sep 18 '16 at 01:44

3 Answers3

6

Nothing seems to have changed because I am able to achieve it even using Swift 3 as shown below.

import UIKit

class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()

        let temp: UIDatePicker = UIDatePicker(frame: self.view.frame)
        temp.setValue(UIColor.purple, forKey: "textColor")

        view.addSubview(temp)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
}
eshirima
  • 3,837
  • 5
  • 37
  • 61
  • On iOS10 I see black text instead of the white I see on iOS9/8. I will attempt to adjust the value outside of my subclass and see if it works. – AppreciateIt Sep 18 '16 at 00:42
  • 1
    You can also set it using the inspector if its not programmatically made. Under **User Defined Runtime Attributes**, you can set the _keypath_ value to `textColor` and _type_ to `Color`. – eshirima Sep 18 '16 at 00:47
  • http://imgur.com/a/tYEQT This is what I see when doing it via storyboard, appears that only the current date is white, while the rest are grey or black. (those that are black should be white, the gray ones are disabled so that is fine) Not sure why those other selectable dates are black text color though. – AppreciateIt Sep 18 '16 at 00:58
  • From your code, you seem to have `setHighlightsToday()`. Are you sure you're not calling it anywhere else? Try commenting it out and any other code that might be tampering with its color – eshirima Sep 18 '16 at 01:03
  • I did a project search for it, there are no other uses of setHighlightsToday(). I commented the single line in my question as well just incase, there is unfortunately no difference. – AppreciateIt Sep 18 '16 at 01:09
  • Two options: 1. Provide the full code of your VC because I am really not able to debug your code with 2. Create an outlet of your picker then try changing it programmatically in your `viewDidLoad` – eshirima Sep 18 '16 at 01:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123617/discussion-between-emil-david-and-appreciateit). – eshirima Sep 18 '16 at 01:26
  • 2
    The UIDatePicker was inside a cell and I was executing the code inside of the init?(coder aDecoder: NSCoder) method. Funnily this had worked in previous iOS versions, but for iOS 10 I had to move it to layoutSubviews() . Now it works fine for all versions of iOS8/9/10. – AppreciateIt Sep 18 '16 at 03:00
  • in case of black text you can adopt appearance to use dark mode: overrideUserInterfaceStyle = .dark but don't forget to do the same for opposite style, in case a client uses another one by default – Zaur_M Jun 22 '21 at 13:47
3

The text colour seems to reset on opening the picker.

I was able to get the text colour to change by using:

setValue(UIColor.white, forKey: "textColor")

after the date picker had appeared on screen. Do it soon enough after and its not noticeable.

I'm not sure how your using your picker, but I had mine set as the input view for a textfield, so was able to use the textfield delegate:

func textFieldDidBeginEditing(textField: UITextField)

to know when the picker had been opened.

I did need to dispatch it with a small delay to take effect correctly.

Update

Instead of using the private textColor property of UIDatePicker, this can be better achieved by using appearances and the public textColor property of UILabel.

UILabel.appearanceWhenContainedInInstancesOfClasses([UIDatePicker.self]).textColor = UIColor.whiteColor()

It still needs to be called after the date picker has been displayed to work.

Community
  • 1
  • 1
Aidan Malone
  • 400
  • 3
  • 7
  • Update for swift 4 -> UILabel.appearance(whenContainedInInstancesOf: [UIDatePicker.self]).textColor = UIColor.white – Peter Suwara Jun 28 '19 at 14:29
1

The code you're using is setting a private property of the UIDatePicker. That's the danger of using a private property. There's no guarantee that it will work in the next OS version.

If Apple detected what you were doing they would have rejected your app.

Unless there is a public interface for doing this, you may be out of luck. (And I'm not aware of a public interface for changing the text color of a date picker.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272