关闭键盘
迅速
Ctrl +从 MainStoryboard 中的 UItextfield 拖动到 ViewController 类并创建一个 UITextField 出口



之后再次选择 UItextField 并在 ViewController 类中按 Ctrl +拖动,但这次选择 Action 连接并在存储上选择 Did End On Exit 然后单击 connect。
在刚刚创建的操作中键入 UItextField .resignFirstResponder() 的名称
   @IBAction func textFieldResign(sender: AnyObject) {
        yourTextFieldName.resignFirstResponder()
    }
当按下键盘上的返回键时,这将隐藏键盘。
按下返回键时隐藏键盘的另一个示例:
我们在 UIViewController 旁边添加了 UITextFieldDelegate 协议
在 vieDidLoad 函数中我们添加了 self.yourTextFieldName.delegate = self
最后我们加上这个
func textFieldShouldReturn(textField: UITextField) -> Bool {
                yourTextFieldName.resignFirstResponder()
                return true
            }
最终的代码是这样的:
class ViewController: UIViewController, UITextFieldDelegate  {
@IBOutlet var textField: UITextField!
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.delegate = self
        }
}
Objective-C
[textField resignFirstResponder];