TSonono
TSonono

Reputation: 45

Change keyboard type of a text field in Swift during runtime

I was wondering if there is a way to check if a text field contains a certain character and if that is the case, change the keyboard type displayed. This is what I have tried but it does not manage to change. This is action occurs when editing the text field is changed.

    @IBAction func decOrNot(_ sender: Any) {
    let cont = "1"
    let field = testKaede.text
    if (field!.contains(cont)) {
        self.testKaede.keyboardType = UIKeyboardType.numberPad
    }
    else {
        self.testKaede.keyboardType = UIKeyboardType.decimalPad
    }
}

Upvotes: 4

Views: 3712

Answers (1)

Robert Dresler
Robert Dresler

Reputation: 11210

Just call reloadInputViews() on your UITextField after you change keyboard's type


textField.keyboardType = .numberPad
textField.reloadInputViews()

Upvotes: 10

Related Questions