ios – 如何将UIDatePicker的背景颜色设置为UITextField inputView?

请不要标记为重复.可用的答案没有回答我的问题.

我使用UIDatePicker作为UITextField的inputView(在UITableViewCell内):

@IBOutlet weak var theTextField: UITextField!

func textFieldDidBeginEditing(_ textField: UITextField) {

        let picker = UIDatePicker()
        picker.setValue(Colors.CI2,forKeyPath: "textColor")

        picker.datePickerMode = .date

        textField.inputView = picker
        textField.inputView?.backgroundColor = Colors.CI1 // my desired color

        picker.addTarget(self,action: #selector(datePickerValueChanged),for: .valueChanged)

}

问题:一旦第二次调用选择器,颜色只会改变.

我想,出现这个问题是因为inputView是可选的,只有在第二次调用picker时,inputView才会在我想要更改颜色的时刻实例化.

我试图继承UITextField并观察inputView.

class DateTextField: UITextField {

    override var inputView: UIView? {

        didSet {

            self.inputView?.backgroundColor = Colors.CI1
            self.reloadInputViews()

        }

    }

}

很遗憾没有成功.相同的行为.我错过了什么?非常感谢帮助.

解决方法

您可以使用以下库来自定义颜色 https://github.com/prolificinteractive/PIDatePicker

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...