Change UIDatePicker TextColor in iOS10

后端 未结 3 594
逝去的感伤
逝去的感伤 2021-01-14 18:24

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 res

3条回答
  •  灰色年华
    2021-01-14 19:12

    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.

提交回复
热议问题