3 Ways to Resign Keyboard in Swift

For the apps that enable users to input something in a textfield, you should always keep an eye on the keyboard resign. Here are 3 of the most common ways to accomplish this goal.

No.1 To resign the first responder position of the keyboard in the buttom action so that press this bottom would also cause keyboard dismiss:

1
self.someTextField.resignFirstResponder()

No.2 To override the touchesBegan function so that if users touch outside the keyboard, the keyboard closes down:

1
2
3
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}

No.3 To inherit UITextFieldDelegate for the current ViewController, then use textFieldShouldReturn function so that when users press return key of the keyboard, the keyboard resigns:

1
2
3
4
func textFieldShouldReturn(textField: UITextField) -> Bool {
someTextField.resignFirstResponder()
return true
}

Just a simple note for myself.