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

前端 未结 12 1480
野趣味
野趣味 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 08:12

    Try this in Swift,

    Step 1: Set delegate as self to your textField

    textField.delegate = self
    

    Step 2: Add this UITextFieldDelegate below your class declaration,

    extension YourClassName: UITextFieldDelegate {
        func textFieldShouldReturn(textField: UITextField) -> Bool {
             textField.resignFirstResponder()
            return true
        }
    }
    

提交回复
热议问题