在iOS10中更改UIDatePicker TextColor

App*_*eIt 3 ios swift

我有一种方法可以UIDatePicker在10之前的iOS版本上更改文本颜色,但是对于iOS10,这些解决方案似乎不再起作用.我怎样才能恢复更改textColorfor 的能力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) 
   }
Run Code Online (Sandbox Code Playgroud)

esh*_*ima 5

似乎没有任何改变,因为我甚至可以使用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()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • UIDatePicker在一个单元格中,我正在执行init?(coder aDecoder:NSCoder)方法中的代码.有趣的是,这在之前的iOS版本中有效,但对于iOS 10,我不得不将其移至layoutSubviews().现在它适用于所有版本的iOS8/9/10. (2认同)