Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift iOS14-如何更改UIKit中UIDatePicker的textColor?_Swift_Uikit_Uidatepicker_Ios14 - Fatal编程技术网

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

Swift iOS14-如何更改UIKit中UIDatePicker的textColor?,swift,uikit,uidatepicker,ios14,Swift,Uikit,Uidatepicker,Ios14,使用iOS14.0.1、Swift5.3、Xcode12.0.1、 我尝试在iOS14中更改UIDatePicker的textColor,并使用UIKit 我了解到,您需要以下方法来执行此操作: 让myDatePicker=UIDatePicker() myDatePicker.tintColor=.white 我还尝试了以下方法(但这会使我的应用程序在iOS14下崩溃): myDatePicker.setValue(UIColor.white,forKeyPath:“textColor”)

使用iOS14.0.1、Swift5.3、Xcode12.0.1、

我尝试在iOS14中更改
UIDatePicker
textColor
,并使用UIKit

我了解到,您需要以下方法来执行此操作:

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

我还尝试了以下方法(但这会使我的应用程序在iOS14下崩溃):

myDatePicker.setValue(UIColor.white,forKeyPath:“textColor”)

我也尝试过(但也没有成功):


在light模式下,我的所有试验都不起作用(如截图摘录所示):


我需要做什么才能在我的
UIDatePicker
中获取白色文本?

我使用
pickerView delegate
方法更改picker视图的颜色。您可以通过添加以下委托方法来添加此函数:
UIPickerViewDelegate、UIPickerViewDataSource

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

      // change your picker view textColor etc. in here..
}

尝试此操作,在下面添加扩展名:

extension UIDatePicker {

var textColor: UIColor? {
    set {
        setValue(newValue, forKeyPath: "textColor")
    }
    get {
        return value(forKeyPath: "textColor") as? UIColor
    }
  }
}
现在在viewDidLoad调用中:

myDatePicker.textColor = .yourColor
在其他选择器属性后调用它,es.:

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")
这就是结果:


是的,它能工作!但是,必须在
textColor=.yourColor
之前设置property
preferredatepickerstyle=.wheels
,否则property
textColor
可能永远不会受到影响。

如果您有此功能的使用案例,请向Apple提交增强请求。谢谢Fabio-我还没有时间测试您的解决方案。您确定它能在iOS14中工作吗?因为
forKeyPath:“textColor”
在iOS14中似乎不再工作了。或者您自己的扩展实现仍然可以吗?@iKK Hi,我在Xcode 12-iOS 14上运行它。。。它就像一个魔术师在我的情况下,它在iOS 14中不起作用。有人找到解决办法了吗?myDatePicker.setValue(true,forKey:“highlightsToday”)在iOS 14中崩溃在iOS 14上崩溃。0@AiyubMunshi只需在声明myDatePicker背景色后调用值键。。。现在没有崩溃,我更新我的答案,谢谢提醒!!!谢谢你mehmett-我也会尝试这个解决方案。。。。
myDatePicker.setValue(true, forKey: "highlightsToday")