How to cancel iOS keyboard programmatically

I created the UITextField programmatically by creating the UITextField a property of the viewController property. I need to remove the keyboard with the return and touch the screen. I managed to turn off the screen, but pressing return does not work.

I saw how to do this using the storyboard and by selecting and initializing the UITextField object directly, without creating it as a property. Can I do it? I'm noob - sorry!

 .h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> @property (strong, atomic) UITextField *username; @end .m #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blueColor]; self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)]; self.username.placeholder = @"Enter your username"; self.username.backgroundColor = [UIColor whiteColor]; self.username.borderStyle = UITextBorderStyleRoundedRect; if (self.username.placeholder != nil) { self.username.clearsOnBeginEditing = NO; } _username.delegate = self; [self.view addSubview:self.username]; [_username resignFirstResponder]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"touchesBegan:withEvent:"); [self.view endEditing:YES]; [super touchesBegan:touches withEvent:event]; } @end 
+81
ios uitextfield keyboard
Sep 12 '13 at 4:31 on
source share
19 answers

A simple way is to connect the UITextField delegate to self ( self.mytestField.delegate = self ) and cancel the keyboard in the textFieldShouldReturn method using [textField resignFirstResponder];

Another way to remove the keyboard:

 [self.view endEditing:YES]; 

Put [self.view endEditing:YES]; where you want to cancel the keyboard (Button event, Touch event, etc.).

+211
Sep 12 '13 at 4:55 on
source share

Add the UITextField delegate UITextField as follows:

 @interface MyController : UIViewController <UITextFieldDelegate> 

And set textField.delegate = self; then add two delegate methods of UITextField

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return YES; } // It is important for you to hide the keyboard - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 
+26
Sep 12 '13 at 4:35
source share

// Hide the keyboard by touching the background image

 - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; } 
+21
Aug 05 '14 at 7:56
source share

just use this in a quick keyboard layout:

 UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil) 

Swift 3

 UIApplication.shared.sendAction(#selector(UIResponder.resignβ€Œβ€‹FirstResponder), to: nil, from: nil, for: nil) 
+11
Nov 10 '15 at 5:16
source share

Try to get the idea that the first responder is in the hierarchy of iOS views. When your text field becomes active (or the first responder), when you touch it (or pass it in the style of messasge becomeFirstResponder programmatically), it represents the keyboard. Therefore, to remove a text field from the first responder, you must pass the message resignFirstResponder .

 [textField resignFirstResponder]; 

And to hide the keyboard on the back button, you must implement your delegation method textFieldShouldReturn: and pass the message resignFirstResponder .

 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } 
+6
Sep 12 '13 at 4:45
source share

Here is what I use in my code. It works like a charm!

In yourviewcontroller.h add:

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

Now in the .m file, add this to your ViewDidLoad function:

 - (void)viewDidLoad { [super viewDidLoad]; //Keyboard stuff tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; tapRecognizer.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapRecognizer]; } 

Also add this function to your .m file:

 - (void)handleSingleTap:(UITapGestureRecognizer *) sender { [self.view endEditing:YES]; } 
+6
Jan 07 '15 at 19:21
source share

In the application delegate, you can write

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.window endEditing:YES]; } 

use this method, you can not write too much code.

+4
Jun 04 '15 at 10:09
source share

For the UITextView group inside a ViewController :

Swift 3.0

 for view in view.subviews { if view is UITextField { view.resignFirstResponder() } } 

Objective-c

 // hide keyboard before dismiss for (UIView *view in [self.view subviews]) { if ([view isKindOfClass:[UITextField class]]) { // no need to cast [view resignFirstResponder]; } } 
+2
Oct 01 '14 at 1:16
source share

To close the keyboard after the keyboard appears, there are 2 cases ,

  • when a UITextField inside a UIScrollView

  • when a UITextField outside of a UIScrollView

2. When the UITextField is outside the UIScrollView override the method in your subclass of the UIViewController

you must also add a delegate for all UITextView

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 
  • In the scroll view, clicking on the outside will not trigger any event , so in this case, use the stiffness handle pointer , Drag and Drop UITapGesture to scroll and create an IBAction for it .

to create an IBAction, press ctrl + click UITapGesture and drag it into the .h viewcontroller file.

Here I called tappedEvent the name of my action

 - (IBAction)tappedEvent:(id)sender { [self.view endEditing:YES]; } 

The information indicated was obtained from the following link, please contact for more information or contact me if you do not understand the abouve data.

http://samwize.com/2014/03/27/dismiss-keyboard-when-tap-outside-a-uitextfield-slash-uitextview/

+2
Dec 02 '15 at 5:18
source share

First you need to add the delegete text box to the .h file. if you do not declare (BOOL)textFieldShouldReturn:(UITextField *)textField this method is not called. First add a delegate and write the code to hide the keyboard in this method.

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

try this one.

+1
Sep 12 '13 at 4:36 on
source share

So, here is what I did to remove it after touching the background or returning. I had to add the delegate = self to viewDidLoad, and then also the delegate methods in the .m files.

 .h #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> @property (strong, atomic) UITextField *username; @end .m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor blueColor]; self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)]; self.username.placeholder = @"Enter your username"; self.username.backgroundColor = [UIColor whiteColor]; self.username.borderStyle = UITextBorderStyleRoundedRect; if (self.username.placeholder != nil) { self.username.clearsOnBeginEditing = NO; } self.username.delegate = self; [self.username resignFirstResponder]; [self.view addSubview:self.username]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } @end 
+1
Sep 12 '13 at 21:23
source share

I know that there were other answers, but I found another article that also covered the absence of a background event - tableview or scrollview.

http://samwize.com/2014/03/27/dismiss-keyboard-when-tap-outside-a-uitextfield-slash-uitextview/

+1
Nov 20 '14 at 23:06
source share

Since the tags speak only iOS, I will post the answer for Swift 1.2 and iOs 8.4, add them to my quick view controller class:

 // MARK: - Close keyboard when touching somewhere else override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { self.view.endEditing(true) } // MARK: - Close keyboard when return pressed func textFieldShouldReturn(textField: UITextField!) -> Bool { textField.resignFirstResponder() return true } // MARK: - 

Also, don't forget to add a UITextFieldDelegate to the class declaration and set the text fields for yourself (view) for your field.

+1
Aug 6 '15 at 16:18
source share

Add Delegate: UITextFieldDelegate

 @interface ViewController : UIViewController <UITextFieldDelegate> 

and then add this delegate method

// This should work fine

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 
0
Sep 12 '13 at 4:53 on
source share
 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[UITextField class]]) { [obj resignFirstResponder]; } }]; } 

when you use more than one text field on the screen With this method you do not need to specify a text field every time, for example

 [textField1 resignFirstResponder]; [textField2 resignFirstResponder]; 
0
Jul 21 '15 at 7:55
source share

Swift 2:

this is what is being done to do everything!

close the keyboard using the Done or Touch outSide , Next button to go to the next input.

First change the TextFiled Return Key to Next in the 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) } 
0
Sep 14 '15 at 7:35
source share

If you don't know the current controller or text view, you can use the responder chain:

 UIApplication.shared.sendAction(#selector(UIView.endEditing(_:)), to:nil, from:nil, for:nil) 
0
Jun 01 '16 at 10:50
source share

for quick 3-4 i fixed type

 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return false } 

just copy the paste anywhere in the class. This solution just works if you want all UItextfield to work the same way, or if you only have one!

0
Dec 13 '17 at 11:18
source share

IN Swift 3

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

OR

 func textFieldShouldReturn(_ textField: UITextField) -> Bool { if textField == yourtextfieldName { self.resignFirstResponder() self.view.endEditing(true) } } 
0
Dec 13 '17 at 16:49
source share



All Articles