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