在运行时更改 Swift 中文本字段的键盘类型

Change keyboard type of a text field in Swift during runtime

我想知道是否有办法检查文本字段是否包含特定字符,如果是这样,请更改显示的键盘类型。这是我尝试过的,但无法改变。这是编辑文本字段发生更改时发生的操作。

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

更改键盘类型后,只需在 UITextField 上调用 reloadInputViews()


textField.keyboardType = .numberPad
textField.reloadInputViews()