在iOS 12中的UIDatePicker中自定义背景和文本颜色



在以前的iOS版本中,我能够使用以下方法更改文本和背景颜色:

let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.date
datePicker.setValue(UIColor.white, forKey: "textColor")
datePicker.backgroundColor = UIColor.black.withAlphaComponent(0.6)

这在 Xcode 10/iOS 12 中不再有效。日期选取器以其默认颜色显示。有没有人找到在iOS 12中自定义文本和背景颜色的解决方案?

似乎在我设置这些值和显示视图之间重置这些值。我能够通过子类化UIDatePicker并在layoutSubviews()中设置值来解决这个问题。可能有一个性能更高的地方来设置颜色,但这现在对我有用。

class DatePicker: UIDatePicker {
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = UIColor.black.withAlphaComponent(0.6)
setValue(false, forKey: "highlightsToday")
setValue(UIColor.white, forKey: "textColor")
}
}

最新更新