Disabling the keyboard when a button is pressed, programmatically with a quick one?

How can I remove the keyboard when I press a button programmatically with a quick one?

+5
source share
4 answers

It all depends on what caused the appearance of the keyboard in the first place. If your page has a UITextField , you can call textField.resignFirstResponder() to hide the keyboard again.

If you used any other object to call the keyboard, just use the link to everything you used and call resignFirstResponder() on that object.

Example:

Suppose you use the button button1 to close the keyboard, and you have textField1 that launches the keyboard.

 button1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped")) 

Then in buttonTappedFunction

 func buttonTapped(){ textField1.resignFirstResponder() } 
+8
source

There is also the so-called "big hammer".

Just call self.view.endEditing(true)

The documentation states the following for this method:

Causes the view (or one of its built-in text fields) to resign the status of the first responder.

+9
source

Setting up a UITextfield delegate programmatically 2.2 Add a UITextFieldDelegate to your class.

fooobar.com/questions/1228737 / ...

+2
source

you need to call resignFirstResponder to the currently active user interface element (most likely UITextField ).

+1
source

All Articles