79

I have a UITextfield that i'd like to dismiss the keyboard for. I can't seem to make the keyboard go away no matter what code i use.

kubi
  • 48,104
  • 19
  • 94
  • 118
Tyler McMaster
  • 1,407
  • 2
  • 14
  • 15
  • Possible duplicate of: [How to close iOS Keyboard by touching anywhere using Swift?](https://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift) (or the opposite, that post is a duplicate of this, with slightly more specific context) – Top-Master Nov 27 '21 at 08:59

16 Answers16

193

If you have multiple text fields and don't know which one is first responder (or you simply don't have access to the text fields from wherever you are writing this code) you can call endEditing: on the parent view containing the text fields.

In a view controller's method, it would look like this:

[self.view endEditing:YES];

The parameter forces the text field to resign first responder status. If you were using a delegate to perform validation and wanted to stop everything until the text field's contents were valid, you could also code it like this:

BOOL didEndEditing = [self.view endEditing:NO];
if (didEndEditing) {
    // on to the next thing...
} else {
    // text field must have said to first responder status: "never wanna give you up, never wanna let you down"
}

The endEditing: method is much better than telling individual text fields to resignFirstResponder, but for some reason I never even found out about it until recently.

benzado
  • 82,288
  • 22
  • 110
  • 138
  • 72
    Damnit! I can't believe I just got rickrolled in a code comment. – Lytol Dec 14 '11 at 20:59
  • @Lytol the point of rickrolling (aka bait-and-switch) is to bait people with a lie, then redirecting them to someone singing "`Never gonna tell a lie`", so we're NOT rickrolled in the slightest (as the lie is missing, it's just an answer with nice joke). – Top-Master Nov 27 '21 at 09:19
65
[myTextField resignFirstResponder]

Here, second paragraph in the Showing and Hiding the Keyboard section.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
11

I've discovered a case where endEditing and resignFirstResponder fail. This has worked for me in those cases.

ObjC

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];    
[self setEditing:NO];

Swift

UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
Community
  • 1
  • 1
Andres Canella
  • 3,706
  • 1
  • 35
  • 47
9

There are cases where no text field is the first responder but the keyboard is on screen. In these cases, the above methods fail to dismiss the keyboard.

One example of how to get there:

  1. push the ABPersonViewController on screen programmatically; open any contact;
  2. touch the "note" field (which becomes first responder and fires up the keyboard);
  3. swipe left on any other field to make the "delete" button appear;
  4. by this point you have no first responder among the text fields (just check programmatically) but the keyboard is still there. Calling [view endEditing:YES] does nothing.

In this case you also need to ask the view controller to exit the editing mode:

[viewController setEditing:NO animated:YES];
user2216794
  • 151
  • 2
  • 2
6

In your view controller YourViewController.h file, make sure you implement UITextFieldDelegate protocol :

@interface YourViewController : <UITextFieldDelegate>

@end

Then, in YourViewController.m file, implement the following instance method:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.yourTextField1 resignFirstResponder];
    [self.yourTextField2 resignFirstResponder];
    ...
    [self.yourTextFieldn resignFirstResponder];
}
Thomas.Benz
  • 8,381
  • 9
  • 38
  • 65
6

I suggest you add and action on your header file:

-(IBAction)removeKeyboard;

And in the implementation, write something like this:

-(IBAction)removeKeyboard
{
[self.textfield resignFirstResponder];
}

In the NIB file, connect from the UITextFiled to the File's Owner on the option DidEndOnExit. That way, when you press return, the keyboard will disappear.

Hope it helps!

ibjazz
  • 263
  • 5
  • 17
4

To resign any text field in the app

UIApplication.shared.keyWindow?.endEditing(true)

This approach is clean and guarantied to work because the keyWindow is, by definition, the root view of all possible views displaying a keyboard (source):

The key window receives keyboard and other non-touch related events. Only one window at a time may be the key window.

Cœur
  • 37,241
  • 25
  • 195
  • 267
3

This will resign one particular text field

// Swift
TextField.resignFirstResponder()

// Objective C
[TextField resignFirstResponder];

To resign any text field use below code

// Swift
self.view!.endEditing(true)

// Objective C
[self.view endEditing:YES];
cyborg86pl
  • 2,597
  • 2
  • 26
  • 43
Milap Kundalia
  • 1,566
  • 1
  • 16
  • 24
3

as a last resort

let dummyTextView = UITextView(frame: .zero)
view.addSubview(dummyTextView)
dummyTextView.becomeFirstResponder()
dummyTextView.resignFirstResponder()
dummyTextView.removeFromSuperview()
Nike Kov
  • 12,630
  • 8
  • 75
  • 122
ochimasu
  • 31
  • 3
  • I had a UI where I had a `UITextField` that was first responder but a popover showing suggestions also active. Closing the keyboard with the hide button did not hide the keyboard somehow. This code helped me here, thanks! :) – heyfrank Apr 30 '19 at 15:07
2

If you don't know which textField is the first responder you can find it. I use this function:

UIView *resignFirstResponder(UIView *theView)
{
    if([theView isFirstResponder])
    {
        [theView resignFirstResponder];
        return theView;
    }
    for(UIView *subview in theView.subviews)
    {
        UIView *result = resignFirstResponder(subview);
        if(result) return result;
    }
    return nil;
}

Then in your code call:

    UIView *resigned = resignFirstResponder([UIScreen mainScreen]);
BadPirate
  • 25,802
  • 10
  • 92
  • 123
1

For Swift 3

You can hide the keyboard like this:

textField.resignFirstResponder()

If you want to hide the keyboard when the user press the "intro" button, you have to implement the following UITextFieldDelegate method:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    textField.resignFirstResponder()

    return true
}
pableiros
  • 14,932
  • 12
  • 99
  • 105
1

You just replace yourTextFieldName with, you guessed it! your textfield. This will close the keyboard.

[yourTextFieldName resignFirstResponder];
theTRON
  • 9,608
  • 2
  • 32
  • 46
A.sharif
  • 1,977
  • 1
  • 16
  • 27
1
-(void)methodName
{
    [textFieldName resignFirstResponder];
}

call this method (methodName) with didEndOnExit

Community
  • 1
  • 1
Prabhjot Singh Gogana
  • 1,408
  • 1
  • 14
  • 39
0
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // your code

    [textField reloadInputViews];
}
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
0

3 Simple & Swift steps

Add UITextFieldDelegate to your class as below:

class RegisterVC: UIViewController, UITextFieldDelegate {
    //class implementation 
}

in class implementation, add the delegate function textFieldShouldEndEditing::

internal func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return true
}

and as a last step, set your UITextField(s) delegate(s) to self, in somewhere appropriate. For example, inside the viewDidLoad function:

override func viewDidLoad(){
    super.viewDidLoad()
    myTextField1.delegate = self
    myTextField2.delegate = self
    ..
    ..
}

Now, whenever user hits the return key, keyboard will dismiss.

I prepared an example snippet too, you can check it from here.

Mert Celik
  • 665
  • 8
  • 12
-1
  1. Set up the "Did End On Exit" event in Xcode (right click on your text field).

    Screenshot

  2. Realize this method:

    -(IBAction) closeKeyboard:(id) sender {
        [_txtField resignFirstResponder];
    }
    
Pang
  • 9,564
  • 146
  • 81
  • 122
ioszhuk
  • 111
  • 3