简体   繁体   English

在iOS10中更改UIDatePicker TextColor

[英]Change UIDatePicker TextColor in iOS10

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. 我有一种方法可以在10之前的iOS版本上更改UIDatePicker的文本颜色,但是对于iOS10,这些解决方案似乎不再起作用。 How could I restore the ability to change the textColor for a UIDatePicker ? 如何恢复更改UIDatePickertextColor的功能?

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

Nothing seems to have changed because I am able to achieve it even using Swift 3 as shown below. 似乎没有任何改变,因为我甚至可以使用Swift 3实现它,如下所示。

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()
    }
}

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: 我不确定你如何使用你的选择器,但我已将我的设置为文本字段的输入视图,因此能够使用textfield委托:

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. 而不是使用UIDatePicker的私有textColor属性,这可以通过使用UILabel的外观和公共textColor属性来更好地实现。

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

It still needs to be called after the date picker has been displayed to work. 在日期选择器显示工作后仍需要调用它。

The code you're using is setting a private property of the UIDatePicker . 您正在使用的代码是设置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. 如果Apple检测到您正在做的事情,他们会拒绝您的应用。

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.) (而且我不知道用于更改日期选择器的文本颜色的公共界面。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM