Changing color of text in datePicker and others

I have seen similar problems reported but find myself unable to fix this.

I have datePicker and a segmented control as subviews in a stack, built on the interface builder. These display on the interface builder with the correct white color text but once the app builds on the simulator, it displays as black text.

I have tried setting the value for the color in viewDidLoad using:

datePicker.setValue(UIColor.white, forKeyPath: "textColor")

but with no luck.

I'm not sure if the fact they are in a subview is the problem, and I'm just missing a tick box somewhere? It's especially confusing that it seems to display properly on the storyboard but as soon as the app loads it reverts to black. Could this be a bug in Xcode?

Many thanks for any help.

Replies

Did you try this, by subclassing ? https://stackoverflow.com/questions/29220535/changing-text-color-of-datepicker

class ColoredDatePicker: UIDatePicker {
    var changed = false
    override func addSubview(view: UIView) {
        if !changed {
            changed = true
            self.setValue(UIColor.whiteColor(), forKey: "textColor")
        }
        super.addSubview(view)
    }
}