Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    }
}
like image 392
TSonono Avatar asked Oct 26 '25 10:10

TSonono


1 Answers

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


textField.keyboardType = .numberPad
textField.reloadInputViews()
like image 129
Robert Dresler Avatar answered Oct 29 '25 01:10

Robert Dresler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!