How to make return key on iPhone make keyboard disappear?

IosIphoneUitextfieldUikeyboard

Ios Problem Overview


I have two UITextFields (e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?

Ios Solutions


Solution 1 - Ios

First you need to conform to the UITextFieldDelegate Protocol in your View/ViewController's header file like this:

@interface YourViewController : UIViewController <UITextFieldDelegate>

Then in your .m file you need to implement the following UITextFieldDelegate protocol method:

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

[textField resignFirstResponder]; makes sure the keyboard is dismissed.

Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:

yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....    
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on

Solution 2 - Ios

Implement the UITextFieldDelegate method like this:

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

Solution 3 - Ios

See Managing the Keyboard for a complete discussion on this topic.

Solution 4 - Ios

Your UITextFields should have a delegate object (UITextFieldDelegate). Use the following code in your delegate to make the keyboard disappear:

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

Should work so far...

Solution 5 - Ios

Took me couple trials, had same issue, this worked for me:

Check your spelling at -

(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];

I corrected mine at textField instead of textfield, capitalise "F"... and bingo!! it worked..

Solution 6 - Ios

When the return key is pressed, call:

[uitextfield resignFirstResponder];

Solution 7 - Ios

After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.

.h

//
//  ViewController.h
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//
    
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;

@end

.m

//
//  ViewController.m
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // _theTextField is the name of the parameter designated in the .h file. 
    _theTextField.returnKeyType = UIReturnKeyDone;
    [_theTextField setDelegate:self];
    
}

// This part is more dynamic as it closes the keyboard regardless of what text field 
// is being used when pressing return.  
// You might want to control every single text field separately but that isn't 
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
}
    

@end

Hope this helps!

Solution 8 - Ios

Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:

-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{
   if (textField == yourTextField) 
   {
      [textField resignFirstResponder]; 
   }
   return NO;
}

Solution 9 - Ios

Add this instead of the pre-defined class

class ViewController: UIViewController, UITextFieldDelegate {

To remove keyboard when clicked outside the keyboard

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

and to remove keyboard when pressed enter

add this line in viewDidLoad()

inputField is the name of the textField used.

self.inputField.delegate = self

and add this function

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

Solution 10 - Ios

Swift 2 :

this is what is did to do every thing !

close keyboard with Done button or Touch outSide ,Next for go to next input.

First Change TextFiled Return Key To Next in StoryBoard.

override func viewDidLoad() {
  txtBillIdentifier.delegate = self
  txtBillIdentifier.tag = 1
  txtPayIdentifier.delegate  = self
  txtPayIdentifier.tag  = 2

  let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture")
  self.view.addGestureRecognizer(tap)
  
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
   if(textField.returnKeyType == UIReturnKeyType.Default) {
       if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField {
           next.becomeFirstResponder()
           return false
       }
   }
   textField.resignFirstResponder()
   return false
}

func onTouchGesture(){
    self.view.endEditing(true)
}

Solution 11 - Ios

in swift you should delegate UITextfieldDelegate, its important don't forget it, in the viewController, like:

class MyViewController: UITextfieldDelegate{

     mytextfield.delegate = self

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

Solution 12 - Ios

You can add an IBAction to the uiTextField(the releation event is "Did End On Exit"),and the IBAction may named hideKeyboard,

-(IBAction)hideKeyboard:(id)sender
{
    [uitextfield resignFirstResponder];
}

also,you can apply it to the other textFields or buttons,for example,you may add a hidden button to the view,when you click it to hide the keyboard.

Solution 13 - Ios

You can try this UITextfield subclass which you can set a condition for the text to dynamically change the UIReturnKey:
https://github.com/codeinteractiveapps/OBReturnKeyTextField

Solution 14 - Ios

If you want to disappear keyboard when writing on alert box textfileds

[[alertController.textFields objectAtIndex:1] resignFirstResponder];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionK.HondaView Question on Stackoverflow
Solution 1 - IosSidView Answer on Stackoverflow
Solution 2 - IosNick WeaverView Answer on Stackoverflow
Solution 3 - IosCalebView Answer on Stackoverflow
Solution 4 - IoscschwarzView Answer on Stackoverflow
Solution 5 - IosEmaView Answer on Stackoverflow
Solution 6 - IosConor TaylorView Answer on Stackoverflow
Solution 7 - Iosuser2904476View Answer on Stackoverflow
Solution 8 - IosAvinash651View Answer on Stackoverflow
Solution 9 - IosAli KahootView Answer on Stackoverflow
Solution 10 - IosMojtaba YeganehView Answer on Stackoverflow
Solution 11 - IosEduardo OliverosView Answer on Stackoverflow
Solution 12 - Ios慭慭流觞View Answer on Stackoverflow
Solution 13 - IosCodeInteractiveView Answer on Stackoverflow
Solution 14 - IosDURGESHView Answer on Stackoverflow