How to hide the keyboard when I press return key in a UITextField?

前端 未结 12 1479
野趣味
野趣味 2020-12-04 07:31

Clicking in a textfield makes the keyboard appear. How do I hide it when the user presses the return key?

12条回答
  •  被撕碎了的回忆
    2020-12-04 07:56

    Swift 4

    Set delegate of UITextField in view controller, field.delegate = self, and then:

    extension ViewController: UITextFieldDelegate {
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            // don't force `endEditing` if you want to be asked for resigning
            // also return real flow value, not strict, like: true / false
            return textField.endEditing(false)
        }
    }
    

提交回复
热议问题