Degtiarev Aleksei
Degtiarev Aleksei

Reputation: 415

How to set keyboardAppearance and inputView?.backgroundColor to dark at the same time?

I have datePicker that I set as inputView of textField.

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)
    }

}

I want to have dark toolbar and dark UIDatePicker's background at the same time. But, actually, textField.inputView?.backgroundColor doesn't work. enter image description here

But if I comment textField.keyboardAppearance = .dark, the background of datepicker changes:

enter image description here

How to fix?

Upvotes: 2

Views: 577

Answers (1)

Kamran
Kamran

Reputation: 15268

I think with datePicker, you don't need this toolBar. You can get rid of it as below,

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 = []
}

If you want toolBar also then you can set a custom toolBar with black background color as below,

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

Upvotes: 1

Related Questions