How to hide the keyboard when I press return key in a UITextField?

145,052

Solution 1

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;

Solution 2

In viewDidLoad declare:

[yourTextField setDelegate:self];

Then, include the override of the delegate method:

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

Solution 3

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
    }
}

Solution 4

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
}

Solution 5

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)
    }
}
Share:
145,052

Related videos on Youtube

aden
Author by

aden

Updated on March 08, 2020

Comments

  • aden
    aden about 4 years

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

    • Felixyz
      Felixyz over 13 years
      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
    • CodeInteractive
      CodeInteractive over 8 years
      This is a subclass of UITextfield that dynamically change the UIReturnKey according to text/string condition: github.com/codeinteractiveapps/OBReturnKeyTextField
    • Iulian Onofrei
      Iulian Onofrei over 3 years
      @Felixyz, The first question there is ironically about Android.
    • Felixyz
      Felixyz over 3 years
      @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.)
    • Iulian Onofrei
      Iulian Onofrei over 3 years
      @Felixyz, Don't worry, I know the feeling :D
  • Deamon
    Deamon about 12 years
    Curious, why is <UITextFieldDelegate> not necessary? Usually you'll see a warning in this case.
  • Saurabh
    Saurabh about 12 years
    if you make your file delegate using Interface Builder then <UITextFieldDelegate> is not necessary in .h file..
  • Piper
    Piper over 10 years
    +1 for the setDelegate: reminder. Definitely had forgotten and code wasn't working previously.
  • orafaelreis
    orafaelreis over 10 years
    that is not the best practice to do that.
  • Clive Jefferies
    Clive Jefferies about 10 years
    Shouldn't this be textField, rather than yourTextField as it is being passed in?
  • oscar castellon
    oscar castellon over 9 years
    is a way of saying that declare the delegate of your textField
  • Umit Kaya
    Umit Kaya almost 9 years
    adding <UITextFieldDelegate> in .h file and set delegate in viewdidload [yourTextField setDelegate:self]; will complete it!
  • Umit Kaya
    Umit Kaya almost 9 years
    <UITextFieldDelegate> should be in .h file, otherwise warning will appear!
  • hris.to
    hris.to over 8 years
    Second step not necessery if you set delegate in IB.
  • Paul Brewczynski
    Paul Brewczynski almost 7 years
    @Saurabh What's so special about storyboard created VC?
  • lal
    lal over 6 years
    Preferred way if using storyboards.
  • Alp Altunel
    Alp Altunel about 6 years
    needs UITextFieldDelegate in the interface definition.
  • Vineesh TP
    Vineesh TP about 6 years
    @AlpAltunel: Thanks for the comment, It was 4 years back.
  • Bryce Sandlund
    Bryce Sandlund about 6 years
    Appears (in current versions of Swift) there should be an underscore before "textField:"
  • Learn2Code
    Learn2Code about 4 years
    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.
  • dimpiax
    dimpiax about 4 years
    @Learn2Code make a manager, collect textfields and chain, the last one will trigger finish function.