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

IosObjective CCocoa TouchDelegates

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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

In viewDidLoad declare:

[yourTextField setDelegate:self];

Then, include the override of the delegate method:

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

Solution 3 - Ios

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

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

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

Solution 6 - Ios

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).

Solution 7 - Ios

Try this,

[textField setDelegate: self];

Then, in textField delegate method

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

Solution 8 - Ios

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

Solution 9 - Ios

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()
	...
}

Solution 10 - Ios

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.

Solution 11 - Ios

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];

Solution 12 - Ios

[textField resignFirstResponder];

Use this

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
QuestionadenView Question on Stackoverflow
Solution 1 - IosSaurabhView Answer on Stackoverflow
Solution 2 - Iososcar castellonView Answer on Stackoverflow
Solution 3 - IosMohammad Zaid PathanView Answer on Stackoverflow
Solution 4 - IosHomamView Answer on Stackoverflow
Solution 5 - IosdimpiaxView Answer on Stackoverflow
Solution 6 - IosMatrixView Answer on Stackoverflow
Solution 7 - IosVineesh TPView Answer on Stackoverflow
Solution 8 - IosiphaawView Answer on Stackoverflow
Solution 9 - IosfecaView Answer on Stackoverflow
Solution 10 - IosAlexView Answer on Stackoverflow
Solution 11 - IosGraycodderView Answer on Stackoverflow
Solution 12 - IosDURGESHView Answer on Stackoverflow