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

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

    You can connect "Primary Action Triggered" (right click on UITextField) with an IBAction and you can resign first responder (without delegation). Example (Swift 4):

    @IBAction func textFieldPrimaryAction(_ sender: UITextField) {
        sender.resignFirstResponder()
        ...
    }
    

提交回复
热议问题