Swift how to resign first responder on all uiTextfield
Or just use a more global approach. (Swift 3.0)
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil);
This will dismiss any active field as it resigns the current first responder.Good luck coding!
You can try this:
for textField in self.view.subviews where textField is UITextField { textField.resignFirstResponder()}
But if you just want to dismiss the Keyboard by pressing the return
on the keyboard
or tapping anywhere on the screen without using touchesBegan
You can try with this:
// For pressing return on the keyboard to dismiss keyboardfunc textFieldShouldReturn(textField: UITextField) -> Bool { for textField in self.view.subviews where textField is UITextField { textField.resignFirstResponder() } return true}
...
func hideKeyboard() { view.endEditing(true)}
And add this to your viewDidLoad
:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))view.addGestureRecognizer(tapGesture)
A simpler approach would by to end editing on the UIView containing the UITextFields by saying:
view.endEditing(true)