如何同时将keyboardAppearance和inputView?.backgroundColor设置为暗?

时间:2018-10-31 22:16:46

标签: ios swift uidatepicker uikeyboard

我有datePicker个设置为inputView的{​​{1}}。

textField

我想同时具有深色工具栏和深色UIDatePicker的背景。但是,实际上import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextField! var datePicker: UIDatePicker = UIDatePicker() override func viewDidLoad() { super.viewDidLoad() datePicker.datePickerMode = .date datePicker.setValue(UIColor.white, forKeyPath: "textColor") textField.inputView = datePicker textField.keyboardAppearance = .dark textField.inputView?.backgroundColor = #colorLiteral(red: 0.07627698034, green: 0.1097967997, blue: 0.1355849206, alpha: 1) } } 不起作用。 enter image description here

但是,如果我评论textField.inputView?.backgroundColor,日期选择器的背景就会改变:

enter image description here

如何解决?

1 个答案:

答案 0 :(得分:1)

我认为使用datePicker,您不需要此toolBar。您可以按照以下说明摆脱它,

override func viewDidLoad() {
    super.viewDidLoad()

    datePicker.datePickerMode = .date
    datePicker.setValue(UIColor.white, forKeyPath: "textColor")
    textField.inputView = datePicker

    datePicker.backgroundColor = .black
    textField.inputAssistantItem.leadingBarButtonGroups = []
    textField.inputAssistantItem.trailingBarButtonGroups = []
}

如果您还想要toolBar,则可以如下设置toolBar背景black的自定义color

let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
toolbar.barTintColor = .black
toolbar.isTranslucent = false // Set to true to give some contrast. 

textField.inputAccessoryView = toolbar