156

Clicking in a textfield makes the keyboard appear. How do I hide it when the user presses the return key?

5
  • 13
    The nice thing about Stack Overflow is that MANY questions have already been answered, especially simple ones. Therefore, please look for answers FIRST, before asking. See, it's simple: stackoverflow.com/search?q=iphone+hide+keyboard
    – Felixyz
    Commented Aug 26, 2010 at 10:57
  • This is a subclass of UITextfield that dynamically change the UIReturnKey according to text/string condition: github.com/codeinteractiveapps/OBReturnKeyTextField Commented Oct 21, 2015 at 1:09
  • @Felixyz, The first question there is ironically about Android. Commented Oct 20, 2020 at 15:26
  • @IulianOnofrei I suppose that might change from year to year. But it's nice to get a reply one decade later :) Not so nice to discover I wrote such a snarky comment ten years ago. (In my defense, perhaps, it wasn't as clear back then that snark is a rot that perpetually threatens to destroy everything that is nice about the internet.)
    – Felixyz
    Commented Oct 27, 2020 at 13:11
  • @Felixyz, Don't worry, I know the feeling :D Commented Oct 27, 2020 at 13:33

12 Answers 12

299

First make your file delegate for UITextField

@interface MYLoginViewController () <UITextFieldDelegate>

@end

Then add this method to your code.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {        
    [textField resignFirstResponder];
    return YES;
}

Also add self.textField.delegate = self;

4
  • 1
    Curious, why is <UITextFieldDelegate> not necessary? Usually you'll see a warning in this case.
    – pixelfreak
    Commented Mar 10, 2012 at 8:59
  • 7
    if you make your file delegate using Interface Builder then <UITextFieldDelegate> is not necessary in .h file..
    – Saurabh
    Commented Mar 10, 2012 at 11:07
  • 1
    adding <UITextFieldDelegate> in .h file and set delegate in viewdidload [yourTextField setDelegate:self]; will complete it!
    – Umit Kaya
    Commented Jul 16, 2015 at 5:35
  • @Saurabh What's so special about storyboard created VC? Commented Jun 20, 2017 at 3:09
63

In viewDidLoad declare:

[yourTextField setDelegate:self];

Then, include the override of the delegate method:

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
4
  • 6
    +1 for the setDelegate: reminder. Definitely had forgotten and code wasn't working previously.
    – Piper
    Commented Jan 18, 2014 at 23:22
  • 2
    Shouldn't this be textField, rather than yourTextField as it is being passed in? Commented Apr 9, 2014 at 9:47
  • is a way of saying that declare the delegate of your textField Commented Oct 17, 2014 at 20:18
  • 1
    <UITextFieldDelegate> should be in .h file, otherwise warning will appear!
    – Umit Kaya
    Commented Jul 16, 2015 at 5:36
24

Try this in Swift,

Step 1: Set delegate as self to your textField

textField.delegate = self

Step 2: Add this UITextFieldDelegate below your class declaration,

extension YourClassName: UITextFieldDelegate {
    func textFieldShouldReturn(textField: UITextField) -> Bool {
         textField.resignFirstResponder()
        return true
    }
}
15

In swift do like this:
First in your ViewController implement this UITextFieldDelegate For eg.

class MyViewController: UIViewController, UITextFieldDelegate {
....
}

Now add a delegate to a TextField in which you want to dismiss the keyboard when return is tapped either in viewDidLoad method like below or where you are initializing it. For eg.

override func viewDidLoad() {

    super.viewDidLoad()   
    myTextField.delegate = self
}

Now add this method.

func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return true
}
2
  • 1
    Second step not necessery if you set delegate in IB.
    – hris.to
    Commented Jan 4, 2016 at 20:41
  • Appears (in current versions of Swift) there should be an underscore before "textField:" Commented May 10, 2018 at 7:14
3

Swift 4

Set delegate of UITextField in view controller, field.delegate = self, and then:

extension ViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        // don't force `endEditing` if you want to be asked for resigning
        // also return real flow value, not strict, like: true / false
        return textField.endEditing(false)
    }
}
2
  • how would you handle wanting users to 'tab' to the next text field with the press of the return key, until the last one and then it resigns ? Also in SwiftUI.
    – Learn2Code
    Commented Mar 15, 2020 at 21:11
  • @Learn2Code make a manager, collect textfields and chain, the last one will trigger finish function.
    – dimpiax
    Commented Mar 16, 2020 at 10:46
2

set delegate of UITextField, and over ride, textFieldShouldReturn method, in that method just write following two lines:

[textField resignFirstResponder];
return YES;

that's it. Before writing a code dont forget to set delegate of a UITextField and set Return key type to "Done" from properties window.(command + shift + I).

0
2

Try this,

[textField setDelegate: self];

Then, in textField delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
3
  • that is not the best practice to do that. Commented Feb 15, 2014 at 17:41
  • needs UITextFieldDelegate in the interface definition. Commented Mar 6, 2018 at 8:47
  • @AlpAltunel: Thanks for the comment, It was 4 years back.
    – Vineesh TP
    Commented Mar 7, 2018 at 3:21
2

Define this class and then set your text field to use the class and this automates the whole hiding keyboard when return is pressed automatically.

class TextFieldWithReturn: UITextField, UITextFieldDelegate
{

    required init?(coder aDecoder: NSCoder) 
    {
        super.init(coder: aDecoder)
        self.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {
        textField.resignFirstResponder()
        return true
    }

}

Then all you need to do in the storyboard is set the fields to use the class:

enter image description here

1

You can connect "Primary Action Triggered" (right click on UITextField) with an IBAction and you can resign first responder (without delegation). Example (Swift 4):

@IBAction func textFieldPrimaryAction(_ sender: UITextField) {
    sender.resignFirstResponder()
    ...
}
1
  • Preferred way if using storyboards.
    – lal
    Commented Jan 24, 2018 at 19:52
1

Ok, I think for a novice things might be a bit confusing. I think the correct answer is a mix of all the above, at least in Swift4.

Either create an extension or use the ViewController in which you'd like to use this but make sure to implement UITextFieldDelegate. For reusability's sake I found it easier to use an extension:

extension UIViewController : UITextFieldDelegate {
    ...
}

but the alternative works as well:

class MyViewController: UIViewController {
    ...
}

Add the method textFieldShouldReturn (depending on your previous option, either in the extension or in your ViewController)

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return textField.endEditing(false)
}

In your viewDidLoad method, set the textfield's delegate to self

@IBOutlet weak var myTextField: UITextField!
...

override func viewDidLoad() {
    ..
    myTextField.delegte = self;
    ..
}

That should be all. Now, when you press return the textFieldShouldReturn should be called.

1

If you want to hide the keyboard for a particular keyboard use [self.view resignFirstResponder]; If you want to hide any keyboard from view use [self.view endEditing:true];

0
[textField resignFirstResponder];

Use this

Not the answer you're looking for? Browse other questions tagged or ask your own question.