使用iOS14.0.1、Swift5.3、Xcode12.0.1、
我试图在textColor中更改UIDatePicker的iOS14,并使用UIKit。
我阅读了here,您需要以下方法才能做到这一点:
let myDatePicker = UIDatePicker()
myDatePicker.tintColor = .white我还尝试了以下方法(但这使我的应用程序在iOS14下崩溃):
myDatePicker.setValue(UIColor.white, forKeyPath: "textColor")我也尝试过(但也没有成功):
UILabel.appearance(whenContainedInInstancesOf: [UIDatePicker.self]).textColor = UIColor.white在光模式下,我的试验都不起作用(如截图-节选中所见):

要在我的UIDatePicker中获得白色文本,我需要做什么?
发布于 2020-10-07 17:44:05
试试这个,添加下面的扩展:
extension UIDatePicker {
var textColor: UIColor? {
set {
setValue(newValue, forKeyPath: "textColor")
}
get {
return value(forKeyPath: "textColor") as? UIColor
}
}
}现在在viewDidLoad调用中:
myDatePicker.textColor = .yourColor在其他选择器属性后面调用它。
myDatePicker.preferredDatePickerStyle = .wheels
myDatePicker.addTarget(self, action: #selector(handleDatePicker), for: .valueChanged)
myDatePicker.datePickerMode = .dateAndTime
myDatePicker.timeZone = NSTimeZone.local
myDatePicker.backgroundColor = .black
myDatePicker.setValue(false, forKey: "highlightsToday")
myDatePicker.textColor = .yourColor // here

如果希望highlightsToday将相对选择器值设置为true:
myDatePicker.setValue(true, forKey: "highlightsToday")这就是结果:

发布于 2021-04-15 03:59:28
是的,很管用!但是,proprety preferredDatePickerStyle = .wheels必须设置在textColor = .yourColor之前,否则proprety textColor永远不会受到影响。
发布于 2020-10-07 15:46:32
我使用pickerView delegate方法改变选择器视图的颜色。您可以通过添加以下委托方法来添加此函数:UIPickerViewDelegate, UIPickerViewDataSource
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
// change your picker view textColor etc. in here..
}https://stackoverflow.com/questions/64247051
复制相似问题