我有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)
}
}
不起作用。
但是,如果我评论textField.inputView?.backgroundColor
,日期选择器的背景就会改变:
如何解决?
答案 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