Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a 'Done' button to numpad keyboard in iOS

So, the numpad keyboard doesn't come with a 'Done' or 'Next' button by default so I'd like to add one. In iOS 6 and below there were some tricks to add a button to the keyboard but they don't seem to be working in iOS 7.

First I subscribe to the keyboard showing notification

[[NSNotificationCenter defaultCenter] addObserver:self                                          selector:@selector(keyboardWillShow:)                                              name:UIKeyboardWillShowNotification                                            object:nil]; 

Then I try to add a button when the keyboard shows up:

- (void)keyboardWillShow:(NSNotification *)note  {     // create custom button     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];     doneButton.frame = CGRectMake(0, 50, 106, 53);     doneButton.adjustsImageWhenHighlighted = NO;     [doneButton setTitle:@"Done" forState:UIControlStateNormal];     [doneButton addTarget:self action:@selector(dismissKeyboard) 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 view found; add the custom button to it         if([[keyboard description] hasPrefix:@"UIKeyboard"] == YES)         [keyboard addSubview:doneButton];     } } 

But the for loop doesn't run because it doesn't find any subviews. Any suggestions? I couldn't find any solutions for iOS7 so is there a different way I'm supposed to be doing this?

Edit: Thanks for all the suggestions for toolbars guys but I'd rather not go down that route as I'm quite space poor (and it is kind of ugly).

like image 959
George McKibbin Avatar asked Nov 25 '13 11:11

George McKibbin


People also ask

How do I add a Done button to a number keypad in Swift?

addDoneKeyboardButton() — creates the keyboard done button using UIToolbar. Inside the toolbar, we create a UIBarButtonItem. You can name the button however you want, and you can add multiple buttons (depending on your needs). In other words, use this function to customize the toolbar.

How do I put the numpad on my iPhone keyboard?

About Numpad Just tap the additional keyboards button to access Numpad.


2 Answers

The much safer approach is to use a UIToolBar with Done Button as inputAccessoryView.


Sample Code :

UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init]; [keyboardDoneButtonView sizeToFit]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"                                                                style:UIBarButtonItemStyleBordered target:self                                                               action:@selector(doneClicked:)]; [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]]; txtField.inputAccessoryView = keyboardDoneButtonView; 

Your -doneClicked method should look like this :

- (IBAction)doneClicked:(id)sender {     NSLog(@"Done Clicked.");     [self.view endEditing:YES]; } 

Sample Code Swift:

let keyboardDoneButtonView = UIToolbar.init() keyboardDoneButtonView.sizeToFit() let doneButton = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.Done,                                                     target: self,                                                     action: Selector("doneClicked:")))      keyboardDoneButtonView.items = [doneButton] textFieldInput.inputAccessoryView = keyboardDoneButtonView 

Your -doneClicked method should look like this :

func doneClicked(sender: AnyObject) {   self.view.endEditing(true) } 
like image 120
Bhavin Avatar answered Sep 28 '22 06:09

Bhavin


Even easier way:

Swift 3.0 and above:

func addDoneButton() {     let keyboardToolbar = UIToolbar()     keyboardToolbar.sizeToFit()     let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,         target: nil, action: nil)     let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done,         target: view, action: #selector(UIView.endEditing(_:)))     keyboardToolbar.items = [flexBarButton, doneBarButton]     textField.inputAccessoryView = keyboardToolbar } 

Swift 2.3 and below:

func addDoneButton() {     let keyboardToolbar = UIToolbar()     keyboardToolbar.sizeToFit()     let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace,         target: nil, action: nil)     let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done,         target: view, action: #selector(UIView.endEditing(_:)))     keyboardToolbar.items = [flexBarButton, doneBarButton]     textField.inputAccessoryView = keyboardToolbar } 

Objective C:

- (void)addDoneButton {     UIToolbar* keyboardToolbar = [[UIToolbar alloc] init];     [keyboardToolbar sizeToFit];     UIBarButtonItem *flexBarButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace     target:nil action:nil];     UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemDone     target:self.view action:@selector(endEditing:)];     keyboardToolbar.items = @[flexBarButton, doneBarButton];     self.textField.inputAccessoryView = keyboardToolbar; } 

EDIT:

I've created a useful library called DCKit, which already have the toolbar out of the box:

Done toolbar above keyboard in iOS (with using DCKit library)

It also has many other cool features.

like image 33
Andrey Gordeev Avatar answered Sep 28 '22 08:09

Andrey Gordeev