通过KVC设置`textColor`时,iOS 14上的UIDatePicker崩溃 [英] UIDatePicker crash on iOS 14 while setting `textColor` via KVC

查看:206
本文介绍了通过KVC设置`textColor`时,iOS 14上的UIDatePicker崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我将UIDatePicker与自定义文本颜色一起使用,并且在iOS 12/13上,此代码可以正常工作:

In my app I use UIDatePicker with custom text color and on iOS 12/13 this code works fine:

timePicker.setValue(UIColor.red, forKeyPath: "textColor")

但是在iOS 14上,我的应用崩溃了.

But on iOS 14 my app crashed.

Stacktrace:

Stacktrace:

-[_UIDatePickerIOSCompactView _setTextColor:]: unrecognized selector sent to instance 0x7ff09265a8c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIDatePickerIOSCompactView _setTextColor:]: unrecognized selector sent to instance 0x7ff09265a8c0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011b0eea2a __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x000000011aa004ce objc_exception_throw + 48
    2   CoreFoundation                      0x000000011b0fd579 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   UIKitCore                           0x00000001235db925 -[UIResponder doesNotRecognizeSelector:] + 292
    4   CoreFoundation                      0x000000011b0f2cf4 ___forwarding___ + 859
    5   CoreFoundation                      0x000000011b0f4f98 _CF_forwarding_prep_0 + 120
    6   Foundation                          0x0000000110c0a6ae -[NSObject(NSKeyValueCoding) setValue:forKey:] + 325
    7   UIKitCore                           0x0000000123af85c5 -[UIView(CALayerDelegate) setValue:forKey:] + 171

有人知道如何在iOS 14+上使用自定义文本颜色吗?

Does anyone know how can I use custom text color on iOS 14+?

推荐答案

快速使用KVC是不安全的,因为如果缺少密钥,您的应用程序将崩溃. 为了更改UIDatePicker中文本的颜色,请使用属性tintColor.

Using KVC is unsafe in swift because if the key is missing, your application will crash. In order to change the color of the text in UIDatePicker, using the property tintColor.

var picker = UIDatePicker()
picker.tintColor = UIColor.blue

这篇关于通过KVC设置`textColor`时,iOS 14上的UIDatePicker崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆