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

Sea*_*dek 5 uidatepicker uikit ios12 xcode10

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

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

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

Sea*_*dek 8

在我设置它们和显示视图之间似乎有什么东西正在重置这些值。我能够通过子类化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")
    }
}
Run Code Online (Sandbox Code Playgroud)