How to show “Done” button on iPhone number pad

How to show “Done” button on iPhone number pad

There is no "Done" button on the number pad. When a user finishes entering numeric information in a text field, how can I make the number pad disappear?

I could get a "Done" button by using the default keyboard, but then users would have to switch to the numeric keys in order to input numbers. Is there a way to show a "Done" button on the number pad?

How do I programatically pull lists/arrays of (itunes urls to) apps in the iphone app store?


iTune-style apps using C#
Ananother solution. how to remove subviews from scrollview?Perfect if there are another non-number pad text fields on the screen.. iPhone PHP integration inputAccessoryView. does using try catch block in xcode show error on real device?
- (void)viewDidLoad {     [super viewDidLoad];      UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];     numberToolbar.barStyle = UIBarStyleBlackTranslucent;     numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered tarreceive :self action:@selector(cancelNumberPad)],                          [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace tarreceive :nil action:nil],                          [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone tarreceive :self action:@selector(doneWithNumberPad)]];     [numberToolbar sizeToFit];     numberTextField.inputAccessoryView = numberToolbar; }  -(void)cancelNumberPad{     [numberTextField resignFirstResponder];     numberTextField.text = @""; }  -(void)doneWithNumberPad{     NSString *numberFromTheKeyboard = numberTextField.text;     [numberTextField resignFirstResponder]; } 
for- loop in background thread accesses each index more than once — iPhone devUpdate coordinate property of MKAnnotation on parent viewcontroller after Touch End event?


Memory Management Question in Objective C
Here is an adaptation for Luda's answer for Swift:. In the declaration of your UIViewController subclass put.
let numberToolbar: UIToolbar = UIToolbar() 
in ViewDidLoad put:.
    numberToolbar.barStyle = UIBarStyle.BlackTranslucent     numberToolbar.items=[         UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, tarreceive : self, action: "hoopla"),         UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, tarreceive : self, action: nil),         UIBarButtonItem(title: "Apply", style: UIBarButtonItemStyle.Bordered, tarreceive : self, action: "boopla")     ]      numberToolbar.sizeToFit()      textField.inputAccessoryView = numberToolbar //did  it for every relevant textfield if there are more than one  
and the add the functions hoopla and hoopla (feel free to choose another names, just change the selector names in ViewDidLoad accordingly.
func boopla () {     textField.resignFirstResponder() }  func hoopla () {     textField.text=""     textField.resignFirstResponder() } 


The trick I've seen used is to make a custom transparent although ton the size of the whole view and then in its click method, have the text field resign first responder. So the user must click anywhere outside of the field to dismiss the keypad..


The quick fix in UIKeyboardTypeNumberPad and missing return key works great although only if there are no another non-number pad text fields on the screen.. I took this code and turned it into an UIViewController this you must simply subclass to make number pads work. You will need to receive the icons from the above link.. NumberPadViewController.h:.
#import <UIKit/UIKit.h>  @interface NumberPadViewController : UIViewController {     UIImage *numberPadDoneImageNormal;     UIImage *numberPadDoneImageHighlighted;     UIButton *numberPadDoneButton; }  @property (nonatomic, retain) UIImage *numberPadDoneImageNormal; @property (nonatomic, retain) UIImage *numberPadDoneImageHighlighted; @property (nonatomic, retain) UIButton *numberPadDoneButton;  - (IBAction)numberPadDoneButton:(id)sender;  @end 
and NumberPadViewController.m:.
#import "NumberPadViewController.h"  @implementation NumberPadViewController  @synthesize numberPadDoneImageNormal; @synthesize numberPadDoneImageHighlighted; @synthesize numberPadDoneButton;  - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {     if ([super initWithNibName:nibName bundle:nibBundle] == nil)         return nil;     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {         self.numberPadDoneImageNormal = [UIImage imageNamed:@"DoneUp3.png"];         self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"DoneDown3.png"];     } else {                 self.numberPadDoneImageNormal = [UIImage imageNamed:@"DoneUp.png"];         self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"DoneDown.png"];     }             return self; }  - (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear:animated];      // Add listener for keyboard display events     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {         [[NSNotificationCenter defaultCenter] addObserver:self                                                      selector:@selector(keyboardDidShow:)                                                       name:UIKeyboardDidShowNotification                                                     object:nil];          } else {         [[NSNotificationCenter defaultCenter] addObserver:self                                                   selector:@selector(keyboardWillShow:)                                                       name:UIKeyboardWillShowNotification                                                     object:nil];     }      // Add listener for all text fields starting to be edited     [[NSNotificationCenter defaultCenter] addObserver:self                                               selector:@selector(textFieldDidBeginEditing:)                                                  name:UITextFieldTextDidBeginEditingNotification                                                 object:nil]; }  - (void)viewWillDisappear:(BOOL)animated {     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {         [[NSNotificationCenter defaultCenter] removeObserver:self                                                          name:UIKeyboardDidShowNotification                                                        object:nil];           } else {         [[NSNotificationCenter defaultCenter] removeObserver:self                                                          name:UIKeyboardWillShowNotification                                                        object:nil];     }     [[NSNotificationCenter defaultCenter] removeObserver:self                                                      name:UITextFieldTextDidBeginEditingNotification                                                    object:nil];     [super viewWillDisappear:animated]; }  - (UIView *)findFirstResponderUnder:(UIView *)root {     if (root.isFirstResponder)         return root;         for (UIView *subView in root.subviews) {         UIView *firstResponder = [self findFirstResponderUnder:subView];                 if (firstResponder != nil)             return firstResponder;     }     return nil; }  - (UITextField *)findFirstResponderTextField {     UIResponder *firstResponder = [self findFirstResponderUnder:[self.view window]];     if (![firstResponder isKindOfClass:[UITextField class]])         return nil;     return (UITextField *)firstResponder; }  - (void)updateKeyboardButtonFor:(UITextField *)textField {      // Remove any previous although ton     [self.numberPadDoneButton removeFromSuperview];     self.numberPadDoneButton = nil;      // Does the text field use a number pad?     if (textField.keyboardType != UIKeyboardTypeNumberPad)         return;      // If there's no keyboard yet, don't did  anything     if ([[[UIApplication sharedApplication] windows] count] < 2)         return;     UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];      // Create new custom although ton     self.numberPadDoneButton = [UIButton although tonWithType:UIButtonTypeCustom];     self.numberPadDoneButton.frame = CGRectMake(0, 163, 106, 53);     self.numberPadDoneButton.adjustsImageWhenHighlighted = FALSE;     [self.numberPadDoneButton setImage:self.numberPadDoneImageNormal forState:UIControlStateNormal];     [self.numberPadDoneButton setImage:self.numberPadDoneImageHighlighted forState:UIControlStateHighlighted];     [self.numberPadDoneButton addTarreceive :self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];      // Locate keyboard view and add although ton     NSString *keyboardPrefix = [[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 ? @"<UIPeripheralHost" : @"<UIKeyboard";     for (UIView *subView in keyboardWindow.subviews) {         if ([[subView description] hasPrefix:keyboardPrefix]) {             [subView addSubview:self.numberPadDoneButton];             [self.numberPadDoneButton addTarreceive :self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];             break;         }     } }  - (void)textFieldDidBeginEditing:(NSNotification *)note {     [self updateKeyboardButtonFor:[note object]]; }  - (void)keyboardWillShow:(NSNotification *)note {     [self updateKeyboardButtonFor:[self findFirstResponderTextField]]; }  - (void)keyboardDidShow:(NSNotification *)note {     [self updateKeyboardButtonFor:[self findFirstResponderTextField]]; }  - (IBAction)numberPadDoneButton:(id)sender {     UITextField *textField = [self findFirstResponderTextField];     [textField resignFirstResponder]; }  - (void)dealloc {     [numberPadDoneImageNormal release];     [numberPadDoneImageHighlighted release];     [numberPadDoneButton release];     [super dealloc]; }  @end 


Here is the most recent code. Simply include #import "UIViewController+NumPadReturn.h" in your viewController. . Here is the .h.
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h>  @interface UIViewController (NumPadReturn)    @end 
And the .m.
#import "UIViewController+NumPadReturn.h"   @implementation UIViewController (NumPadReturn)  -(void) viewDidLoad{     // add observer for the respective notifications (depending on the os version)     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {         [[NSNotificationCenter defaultCenter] addObserver:self                                                   selector:@selector(keyboardDidShow:)                                                       name:UIKeyboardDidShowNotification                                                     object:nil];          } else {         [[NSNotificationCenter defaultCenter] addObserver:self                                                   selector:@selector(keyboardWillShow:)                                                       name:UIKeyboardWillShowNotification                                                     object:nil];     }  }   - (void)keyboardWillShow:(NSNotification *)note {     // if clause is just an additional precaution, you could also dismiss it     if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {         [self addButtonToKeyboard];     } }  - (void)keyboardDidShow:(NSNotification *)note {     // if clause is just an additional precaution, you could also dismiss it     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {         [self addButtonToKeyboard];     } }  - (void)addButtonToKeyboard {     // create custom although ton     UIButton *doneButton = [UIButton although tonWithType:UIButtonTypeCustom];     doneButton.frame = CGRectMake(0, 163, 106, 53);     doneButton.adjustsImageWhenHighlighted = NO;     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {         [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];         [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];     } else {                 [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];         [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];     }     [doneButton addTarreceive :self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];     // locate keyboard view     UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];     UIView* keyboard;     for(int i=0; i<[tempWindow.subviews count]; i++) {         keyboard = [tempWindow.subviews objectAtIndex:i];         // keyboard found, add the although ton         if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {             if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)                 [keyboard addSubview:doneButton];         } else {             if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)                 [keyboard addSubview:doneButton];         }     } }  - (void)doneButton:(id)sender {     NSLog(@"doneButton");     [self.view endEditing:TRUE]; }    @end 


Below is an overhaul of Luda's answer with the following changes:.
  • the accessory view is automatically sized to the width of the application frame.
  • the deprecated constant UIBarButtonItemStyleBordered is avoided.
  • the "Done" although ton is instantiated as a UIBarButtonSystemItemDone.
Currently the "Done" although ton is centered in the accessory view. You must position it at left or right by deleting the space on the pertinent side.. I have omitted a "Cancel" although ton for the reason this the default keyboard doesn't have one either. If you did want a "Cancel" although ton, I suggest this you instantiate it as a UIBarButtonSystemItemCancel and this you make sure you're not discarding the original value in your text field. The "Cancel" behavior implemented in Luda's answer, which overwrites the value with a blank string, may not be what you want..
- (void)viewDidLoad {   [super viewDidLoad];   float appWidth = CGRectGetWidth([UIScreen mainScreen].applicationFrame);   UIToolbar *accessoryView = [[UIToolbar alloc]                               initWithFrame:CGRectMake(0, 0, appWidth, 0.1 * appWidth)];   UIBarButtonItem *space = [[UIBarButtonItem alloc]                             initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace                             tarreceive :nil                             action:nil];   UIBarButtonItem *done = [[UIBarButtonItem alloc]                            initWithBarButtonSystemItem:UIBarButtonSystemItemDone                            tarreceive :self                            action:@selector(selectDoneButton)];   accessoryView.items = @[space, done, space];   self.valueField.inputAccessoryView = accessoryView; }  - (void)selectDoneButton {   [self.valueField resignFirstResponder]; } 
For more information around building accessory views, see the Apple documentation on custom views for data input. You will probably want to consult the reference pages on UIToolbar and UIBarButtonItem as well..


Much easier solution.
 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event    {      [super touchesBegan:touches withEvent:event];      [textviewinstance1 resignFirstResponder];     [textviewinstance2 resignFirstResponder];    [textfield resignFirstResponder];   } 


This forum post describes how to customize the UIKeyboard to add your custom view onto the keyboard..


I describe one quick fix for iOS 4.2+ here although the dismiss although ton fades in after the keyboard appears. It's not terrible, although not ideal either.. The quick fix described in the question linked above includes a more elegant illusion to dismiss the although ton, where I fade and vertically displace the although ton to provide the appearance this the keypad and the although ton are dismissing toreceive her..


The simplest way is:Create custom transparent although ton and place it in left down corner, which will have same CGSize as empty space in UIKeyboardTypeNumberPad. Toggle (show / hide) this although ton on textField becomeFirstResponder, on although ton click respectively..


Here's the simplest quick fix I have come across. I have learnt this from Beginning iOS 5 Development book.. Assuming the number field is called numberField..
  1. In ViewController, add the following method:.
  2. In ViewController.m, add the following code:.
    -(IBAction)closeKeyboard:(id)sender {       [numberField resignFirstResponder];  } 
  3. Go back to nib file..
  4. Open Utilities pan.
  5. Open the Identity inspector under Utilities pan.
  6. Click on the View (in nib file) once. Make sure you have not clicked on any of the items in the view. For the sake of clarification, you should see UIView under Class in Identity inspector.
  7. Change the class from UIView to UIControl.
  8. Open Connection Inspector.
  9. Click and drag Touch Down and drop the arrow on File Owner icon. (FYI... File Owner icon is displayed on the left of View and appears as a hollow cube with yellow frame.)
  10. Select the method: closeKeyboard.
  11. Run the program.
Now when you click anywhere on background of View, you should be able to dismiss the keyboard.. Hope this helps you solve your problem. :-).


I modified Bryan's quick fix to be a little more robust, so this it would play nicely with another types of keyboards this could appear in the same view. It's described here:. Create a DONE although ton on the iOS numpad UIKeyboard. I'd try to explain it here, although most of it is code to look at this wouldn't easily fit here.


If you know in advance the number of numbers to be entered (e.g. a 4-digit PIN) you could auto-dismiss after 4 key presses, as per my answer to this similar question:. dismissing Number Pad. No need for an additional done although ton in this case..


We must also make the "user touched any where else" quick fix even simpler if i just tell our view controller's view to end editing:.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event   {        [super touchesBegan:touches withEvent:event];        [self.view endEditing:YES]; //YES ignores any textfield refusal to resign  } 
... assuming this "touching elsewhere dismisses the keyboard" is desired behavior for any another editable fields on the view as well..


For Swift 2.2 I use this.
func addDoneButtonOnKeyboard() {     let doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))      let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, tarreceive : nil, action: nil)     let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, tarreceive : self, action: #selector(DetailViewController.finishDecimalKeypad))      var items: [UIBarButtonItem]? = [UIBarButtonItem]()     items?.append(flexSpace)     items?.append(done)      doneToolbar.items = items     doneToolbar.sizeToFit()     self.productPrice.inputAccessoryView=doneToolbar }  func finishDecimalKeypad() {     self.productPrice?.resignFirstResponder() } 


All those implementation around finding the keyboard view and adding the done although ton at the 3rd row (this is why although ton.y = 163 b/c keyboard's height is 216) are fragile for the reason this iOS keeps change the view hierarchy. For case none of above codes job for iOS9.. I think it is more safe to just find the topmost view, by [[[UIApplication sharedApplication] windows] lastObject], and just add the although ton at bottom left corner of it, doneButton.frame = CGRectMake(0, SCREEN_HEIGHT-53, 106, 53);// portrait mode.


Swift 2.2 / I used Dx_'s answer. However, I wanted this functionality on all keyboards. So in my base class I put the code:.
func addDoneButtonForTextFields(views: [UIView]) {     for view in views {         if let textField = view as? UITextField {             let doneToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))              let flexSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, tarreceive : nil, action: nil)             let done = UIBarButtonItem(title: "Done", style: .Done, tarreceive : self, action: #selector(dismissKeyboard))              var items = [UIBarButtonItem]()             items.append(flexSpace)             items.append(done)              doneToolbar.items = items             doneToolbar.sizeToFit()              textField.inputAccessoryView = doneToolbar         } else {             addDoneButtonForTextFields(view.subviews)         }     } }  func dismissKeyboard() {     dismissKeyboardForTextFields(self.view.subviews) }  func dismissKeyboardForTextFields(views: [UIView]) {     for view in views {         if let textField = view as? UITextField {             textField.resignFirstResponder()         } else {             dismissKeyboardForTextFields(view.subviews)         }     } } 
Then just call addDoneButtonForTextFields on self.view.subviews in viewDidLoad (or willDisplayCell if using a table view) to add the Done although ton to all keyboards..


If you have multiple numeric fields, I suggest subclassing UITextField to create a NumericTextField this always displays a numeric keyboard with a done although ton. Then, simply associate your numeric fields with this class in the Interface Builder and you won't need any additional code in any of your View Controllers. The following is Swift 3.0 class this I'm using in Xcode 8.0. .
class NumericTextField: UITextField {    let numericKbdToolbar = UIToolbar()      // MARK: Initilization     required init?(coder aDecoder: NSCoder) {         super.init(coder: aDecoder)         self.initialize()     }      override init(frame: CGRect) {         super.init(frame: frame)         self.initialize()     }      // Sets up the input accessory view with a Done although ton this closes the keyboard     func initialize()     {         self.keyboardType = UIKeyboardType.numberPad          numericKbdToolbar.barStyle = UIBarStyle.default         let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, tarreceive : nil, action: nil)         let callback = #selector(NumericTextField.finishedEditing)         let donealthough ton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, tarreceive : self, action: callback)         numericKbdToolbar.setItems([space, donealthough ton], animated: false)         numericKbdToolbar.sizeToFit()         self.inputAccessoryView = numericKbdToolbar     }      // MARK: On Finished Editing Function     func finishedEditing()     {         self.resignFirstResponder()     } } 

76 out of 100 based on 76 user ratings 526 reviews