Dismiss keyboard after pressing Return
Set the view controller delegate to an object which implements the method textFieldShouldReturn(_) in the protocol UITextFieldDelegate.
Swift 2.3
// MARK: UITextFieldDelegate
/**
When the user hits the return or "Done" button on a text field, we want
the keyboard to go away.
*/
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
h/t
- Apple’s sample code “MyLife” PersonViewController.swift: