How to dismiss number pad keyboard by tapping anywhere

58,708

Solution 1

Declaring the text field as instance variable or property if it isn't already and implementing a simple method like this:

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

will do just fine.

Solution 2

Try this method,

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

Now tap anywhere and see keyboard will dismiss. :-)

Solution 3

For Swift Use Below Code

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

Check out latest update on following Link

Solution 4

Touching the Background to Close the Keyboard

Go to Xcode if you’re not already there. We need to add one more action to our controller class. Add the following line to your 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; switch over to the implementation file, and add this code, which simply tells both text fields to yield first responder status if they have it. It is perfectly safe to call resignFirstResponder on a control that is not the first responder, so we can safely call it on both text fields without having to check whether either is the first responder.

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

TIP Save this file, and go back to Interface Builder. We now need to change the underlying class of our nib’s view. If you look at the nib’s main window , you’ll see that there are three icons in that view. The third one, called View, is our nib’s main view that holds all the other controls and views as subviews. Single-click the icon called View, which represents our nib’s container view. Press ␣4 to bring up the identity inspector. This is where we can change the underlying class of any object instance in Interface Builder.

You’ll be switching between header and implementation files a lot as you code. Fortunately, Xcode has a key combination that will switch you between these files quickly. The default key combination is ␣␣␣ (option-command-up arrow), although you can change it to anything you want using Xcode’s preferences.76

The field labeled Class currently says UIView. Change it to read UIControl. All controls that are capable of trig- gering action methods are subclasses of UIControl, so by changing the underlying class, we have just given this view the ability to trigger action methods. You can verify this by pressing ␣2 to bring up the connections inspector.

Drag from the Touch Down event to the File’s Owner icon, and choose the backgroundTap: action. Now, touches anywhere in the view without an active control will trigger our new action method, which will cause the keyboard to retract.

NOTE Save the nib, and let’s go back and try it. Compile and run your application again. This time, the keyboard should disappear not only when the Done button is tapped but also when you click anywhere that’s not an active control, which is the behavior that your user will expect.

Solution 5

You can implement @mbm's answer really nicely by putting a didSet on the outlet and attaching your inputAccessoryView there:

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

In xcode 8 / iOS 10 It ends up looking something like this:

enter image description here

Share:
58,708
jsanmtosj
Author by

jsanmtosj

Updated on July 09, 2022

Comments

  • jsanmtosj
    jsanmtosj almost 2 years

    I'd like to know the simplest code to dismiss the number pad keyboard when tapping anywhere outside the number pad. It's a simple application to input a number inside a text field and then a user can dismiss the keyboard after the user finishes typing the numbers on the text field. Thanks! :)

  • jsanmtosj
    jsanmtosj over 12 years
    Thanks for this sir rptwsthi! :) Appreciate your post.
  • jsanmtosj
    jsanmtosj over 12 years
    Thanks for this fichek. This is the easiest way so far to dismiss the keyboard. :)
  • ragnarius
    ragnarius almost 10 years
    It does not work so well if the user taps another control (and not the background).
  • Javal Nanda
    Javal Nanda over 9 years
    Is there any way to handle the dismiss of number pad when external bluetooth keyboard is attached? Hitting return key on external keyboard does not trigger textFieldShouldReturn delegate as there is no return key for number pad
  • TheCodingArt
    TheCodingArt over 9 years
    I have no clue how I missed this API call, but this is the most correct answer here.
  • Zack Shapiro
    Zack Shapiro over 9 years
    Thank you. Glad to see a Swift answer in here
  • user3731622
    user3731622 about 8 years
    touchesBegan has been updated. If you have problems, checkout stackoverflow.com/questions/28771896/….
  • Jeeyoung Kim
    Jeeyoung Kim over 7 years
    Now the function reads override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
  • RowanPD
    RowanPD over 4 years
    Underappreciated answer. I like it.
  • Włodzimierz Woźniak
    Włodzimierz Woźniak over 3 years
    the best one solution!