How to reject a numeric keypad by clicking anywhere

I would like to know the simplest code to dismiss a numeric keypad when you click anywhere outside the numeric keypad. This is a simple application for entering numbers inside a text field, and then the user can cancel the keyboard after the user has finished entering numbers in the text field. Thank you :)

+47
ios numbers iphone uitextfield xcode
Aug 09 '11 at 3:30 a.m.
source share
15 answers

Declaring a text field as an instance variable or property if it is not already implemented and implements a simple way:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [textField resignFirstResponder]; } 

will be wonderful.

+83
Aug 09
source share
— -

Try this method

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

Now click anywhere and you will see that the keyboard will be disabled. :-)

+20
Feb 26 '14 at 3:02
source share

For quick use of the code below

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { textFiled.resignFirstResponder() } 

Check out the latest update after Link

+7
Oct 29 '14 at 7:19
source share

Touching the background to close the keyboard

Go to Xcode if you haven't already. We need to add one more action to our controller class. Add the following line to the Control_FunViewController.h file:

 #import <UIKit/UIKit.h> @interface Control_FunViewController : UIViewController { UITextField *nameField; UITextField *numberField; } @property (nonatomic, retain) IBOutlet UITextField *nameField; ] @property (nonatomic, retain) IBOutlet UITextField *numberField; - (IBAction)textFieldDoneEditing:(id)sender; - (IBAction)backgroundTap:(id)sender; @end 

Save the header file; go to the implementation file and add this code that just tells both text fields to get the status of the first responder, if he has one. It is perfectly safe to call resignFirstResponder on a control that is not the first responder, so we can safely call it in both text fields without checking if it is the first responder.

 - (IBAction)backgroundTap:(id)sender { [nameField resignFirstResponder]; [numberField resignFirstResponder]; } 

TIP Save this file and return to Interface Builder. Now we need to change the base class of the nibs view. If you look at the main nibs window, you will see that there are three icons in this view. The third, called View, is the main nibs view, which contains all the other controls and views as subzones. Click the View icon once, which is a view of the nibs container. Press ␣4 to call up the identity inspector. Here we can change the base class of any instance of the object in Interface Builder.

You will repeatedly switch between header files and implementation during encoding. Fortunately, Xcode has a keyboard shortcut that quickly switches you between these files. The default keyboard shortcut is ␣␣␣ (option-command-arrow), although you can change it to whatever you want using Xcodes preferences.

The box labeled Class is currently specifying UIView. Change it to read UIControl. All controls that can trigger action methods are subclasses of UIControl, so by changing the base class, we just gave this view the ability to run action methods. You can check this by pressing ␣2 to call the connection inspector.

Drag from the Touch Down event to the File Owner icon and select the backgroundTap: action. Now, touching anywhere in the view without an active control will call our new action method, which will make the keyboard fall back.

Note Save the tip and go back and try. Compile and run the application again. This time, the keyboard should disappear not only when the “Finish” button is pressed, but also when you click anywhere where there is no active control, and this is the behavior that your user will expect.

+6
Aug 09 2018-11-11T00:
source share

I tried to implement some of the solutions here and found that there were some problems with them. Notably, My UIView contains a UIScrollView , which appears to intercept strokes.

When this failed, I thought that “listening anywhere” is a bit vague behavior, requiring the user to guess how to remove the keyboard and not give them clear directions.

In addition, I have a “Return” or “Finish” button on every other keyboard that I show in the application, so I decided to give the user a simple, intuitive, well-defined way to reject the keyboard using the Pad number, which corresponded to the other mechanisms used keyboard firing.

I understand that this is not what the OP specifically requests, but I think this is a better solution than the “touch anywhere” request (and it’s easy to implement!).

The following code is called the -viewDidLoad part in my UIViewController .

 // My app is restricted to portrait-only, so the following works UIToolbar *numberPadAccessoryInputView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)]; // My app-wide tint color is a gold-ish color, so darkGray contrasts nicely numberPadAccessoryInputView.barTintColor = [UIColor darkGrayColor]; // A basic "Done" button, that calls [self.textField resignFirstResponder] UIBarButtonItem *numberPadDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self.textField action:@selector(resignFirstResponder)]; // It the only item in the UIToolbar items array numberPadAccessoryInputView.items = @[numberPadDoneButton]; // In case the background of the view is similar to [UIColor darkGrayColor], this // is put as a contrasting edge line at the top of the UIToolbar UIView *topBorderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, numberPadAccessoryInputView.frame.size.width, 1.0f)]; topBorderView.backgroundColor = [UIColor whiteColor]; [numberPadAccessoryInputView addSubview:topBorderView]; // Make it so that this UITextField shows the UIToolbar self.textField.inputAccessoryView = numberPadAccessoryInputView; 

At the same time, a nice, beautiful, intuitive control has been added to the top of the keyboard, which clearly indicates how the user should act when he finishes his text.

I still believe that Apple should provide a Finish button for this keyboard (for example, a link posted by Janak Nirmal), but this is the most elegant solution other than Apple.

+3
02 Sep '14 at 13:36 on
source share

To achieve this, you must use the UITapGestureRecognizer.

 UITapGestureRecognizer *tapToDismiss = [[UITapGestureRecognizer alloc]initWithTarget: self action: @selector (dismissNumberKeyBoard:)]; 

Then in your rejectNumberKeyboard method

 -(Void)dismissNumberKeyboard : (id)sender { [yourTextField resignFirstResponder]; } 

Happy coding!

+2
Feb 11 '15 at 13:42
source share

Just create a simple function like this.

  -(IBAction)hideKeyboard:(id)Sender { [_textValue resignFirstResponder]; } 

now go to the ur nib file and connect this function with a text field using didEndOnExit and you are good to go. Now, when u selects the ur external text field, the keyboard will be hidden.

+1
Aug 09 2018-11-11T00:
source share

You can perfectly implement @mbm's answer by placing didSet on a socket and adding your input access there:

Quick code:

  @IBOutlet var input: UITextField! { didSet { let toolbar = UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 0, height: 44))) toolbar.items = [ UIBarButtonItem(barButtonSystemItem: .done, target: self.input, action: #selector(resignFirstResponder)) ] input.inputAccessoryView = toolbar }} 

On xcode 8 / iOS 10 As a result, it looks something like this:

enter image description here

+1
Sep 02 '16 at 0:56
source share

Usually, when the user touches the Cancel button, I use this to cancel the keyboard -

 - (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar { NSLog(@"cancel clicked"); [searchBar resignFirstResponder]; // dismiss keyboard return; } 

If you want to do this when the user touches somewhere outside the keyboard area, you need to implement touchesEnded and put this code there -

 /* Delegate for when touch ends. */ -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [searchBar resignFirstResponder]; } 

I have not tried this myself, but I believe that this should do the trick ...

0
Aug 09 '11 at 3:45 a.m.
source share

I had to implement a similar UX for my UITableView (inside which a UITextField appears). First add a UITapGestureRecognizer to the tableView. This will begin to grab all the cranes that occur from now on.

 UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [self.tableView addGestureRecognizer:tapGestureRecognizer]; [tapGestureRecognizer release]; 

However, the side effect is that you do not want to reject the keyboard when the user clicks on the UITextField. So, this case needs to be distinguished.

The implementation of the handleTap method looks something like this:

 /* In the text editing mode, listens to all taps occurring on the table view to exit the mode. There are two special cases to consider: a) Text field clear button b) Text field */ -(void)handleTap:(UITapGestureRecognizer*)tapRecognizer { if(tapRecognizer.state == UIGestureRecognizerStateEnded) { //Figure out where the user tapped CGPoint p = [tapRecognizer locationInView:textField]; CGRect textFieldBounds = textField.bounds; CGRect clearButtonBounds = CGRectMake(textFieldBounds.origin.x + textFieldBounds.size.width - 44, textFieldBounds.origin.y, 44, textFieldBounds.size.height); if(CGRectContainsPoint(clearButtonBounds, p)) textField.text = @""; if(CGRectContainsPoint(textFieldBounds, p)) return; [textField resignFirstResponder]; //remove the tap gesture recognizer that was added. for(id element in self.tableView.gestureRecognizers) { if([element isKindOfClass:[UITapGestureRecognizer class]]) { [self.tableView removeGestureRecognizer:element]; } } } [self commitNewText]; } } 

NTN. Mark this as an answer if you like it.

-Akshay

0
Aug 09 2018-11-11T00:
source share

If you created a custom dashboard or other popover, another solution would be to use the gesture recognizer in the init of your UIView as follows:

 tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; tapper.cancelsTouchesInView = NO; [self addGestureRecognizer:tapper]; _yourViewController = [[CustomViewController alloc] init]; _yourPopoverController = [[UIPopoverController alloc] initWithContentViewController:_yourViewController]; _yourPopoverController.popoverContentSize = CGSizeMake(550, 300); _yourViewController.delegate = self; 

And the handleSingleTap descriptor rejects the popover, if visible, and rejects the edit:

 - (void)handleSingleTap:(UITapGestureRecognizer *) sender { if ([_yourPopoverController isPopoverVisible]) { [_yourPopoverController dismissPopoverAnimated:YES]; } [self endEditing:YES]; } 
0
Feb 26 '14 at 9:07
source share

In xcode 5, use the stiffness recognition flag and declare in .h with action, select a name and in .m

 - (IBAction)backgroundTap:(id)sender { [self.view endEditing: YES]; } 
0
Aug 26 '14 at 18:09
source share

There are many answers here, but still had to deal with this using Xcode 5. Read this: https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html

Add a gesture recognition attribute to your UIScrollView. Set it as a delegate as a viewcontroller.

Add this method:

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

to your UIScrollView implementation code and associate it with the Recognizer Tap Gesture by clicking on the control and dragging it into the implementation code.

Worked for me.

0
Jan 18 '15 at 2:33
source share

For Swift, the easiest way to drop the keyboard for all text fields is by clicking anywhere outside the text field:

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { self.view.endEditing(true) } 
0
02 Feb. '15 at 6:34
source share

For quick 3/4, I like this case:

  • View Controller is a subclass of UITextFieldDelegate
  • In the viewDidLoad function, you must set yourPhonePadField.delegate = self
  • And override this function:

     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { self.view.endEditing(true) } 
0
Jan 22 '18 at 17:10
source share



All Articles