uikit - 在 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")
}
}
推荐阅读
- python - Python 请求、工作代理和 Django REST
- python - pygame中的3D投影
- css - Primeflex + PrimeNG 选项卡 + PrimeNG 树 - 无法使 tabPanel 增长
- java - 如何发布 maven:准备问题和未找到插件问题
- javascript - 使用 HTML 表单和 js 代码将数据发送到电子表格
- android - 任何课程的 kotlin 课程
- sql - How to Update of multiple lines with join in PostgreSQL
- z3 - z3: Horn clauses / Fixedpoints over inductive datatypes
- c++ - 将指针传递给 C++ 中的方法导致奇怪的输出
- assembly - How to compare negative numbers to positive numbers in MIPs assembly