Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to dismiss keyboard?

I have quite a few controls scattered throughout many table cells in my table, and I was wondering if there's an easier way to dismiss the keyboard without having to loop through all my controls and resigning them all as the first responder. I guess the question is.. How would I get the current first responder to the keyboard?

like image 804
Seventoes Avatar asked Apr 12 '09 02:04

Seventoes


People also ask

How do you dismiss a keyboard?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do you dismiss a keyboard in Objective C?

You can force the currently-editing view to resign its first responder status with [view endEditing:YES] . This hides the keyboard.

How do I dismiss my keyboard on my Iphone?

To hide it, slide your finger down from above the text-entry box and the keyboard will start to disappear.

How do I dismiss the keyboard for a TextField?

If you're supporting only iOS 15 and later, you can activate and dismiss the keyboard for a text field by focusing and unfocusing it. In its simplest form, this is done using the @FocusState property wrapper and the focusable() modifier – the first stores a Boolean that tracks whether the second is currently focused.


2 Answers

Try:

[self.view endEditing:YES]; 
like image 136
kirby Avatar answered Sep 21 '22 13:09

kirby


You can force the currently-editing view to resign its first responder status with [view endEditing:YES]. This hides the keyboard.

Unlike -[UIResponder resignFirstResponder], -[UIView endEditing:] will search through subviews to find the current first responder. So you can send it to your top-level view (e.g. self.view in a UIViewController) and it will do the right thing.

(This answer previously included a couple of other solutions, which also worked but were more complicated than is necessary. I've removed them to avoid confusion.)

like image 32
Nicholas Riley Avatar answered Sep 25 '22 13:09

Nicholas Riley