gpt4 book ai didi

swift - iOS14 - 如何在 UIKit 中更改 UIDatePicker 的 textColor?

转载 作者:行者123 更新时间:2023-12-03 23:43:39 25 4
gpt4 key购买 nike

使用 iOS14.0.1、Swift5.3、Xcode12.0.1、
我尝试更改 textColorUIDatePicker在 iOS14 中并使用 UIKit。
我读了 here你需要以下方法来做到这一点:

let myDatePicker = UIDatePicker()
myDatePicker.tintColor = .white

我还尝试了以下操作(但这会使我的应用程序在 iOS14 下崩溃):
myDatePicker.setValue(UIColor.white, forKeyPath: "textColor")

我也尝试过(但也没有成功):
UILabel.appearance(whenContainedInInstancesOf: [UIDatePicker.self]).textColor = UIColor.white

在光照模式下,我的所有试验都不起作用(如截图摘录中所示):
enter image description here
我需要做什么才能在我的 UIDatePicker 中获得白色文本?

最佳答案

试试这个,在下面添加扩展名:

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
enter image description here
如果你想 highlightToday 将相对选择器值设置为 true:
myDatePicker.setValue(true, forKey: "highlightsToday")
这是结果:
enter image description here

关于swift - iOS14 - 如何在 UIKit 中更改 UIDatePicker 的 textColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64247051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com