64

I want to have a keyboard which has a Next,Previous and Done button on top of it.

I have seen that in many apps.

Especially where there are forms to be filled.

enter image description here

I want to achieve something similar to above keyboard

How can I get that?

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216

8 Answers8

60

You'll find the answer on this other post. I checked the iOS Library and the inputAccessoryView of a UITextField is exactly what you're looking for.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ermiar
  • 1,078
  • 13
  • 20
  • Or it can be done using storyboard like [here](http://stackoverflow.com/a/41546987/1151916) – Ramis Jan 09 '17 at 11:29
25

I just created a class called BSKeyboardControls which makes it very easy to add the controls to a keyboard. The class, instructions and example code can be found here at GitHub.

The controls works for text fields and text views and are optimized for both iPhone and iPad.

simonbs
  • 7,932
  • 13
  • 69
  • 115
  • Just now discovering BSkeyboardControls and love it! Need it to work with a UIDatePicker instead of a textfield. Any sample code for this? – phil Dec 25 '14 at 18:45
19
-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField 

{
     UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];

    keyboardToolBar.barStyle = UIBarStyleDefault;
    [keyboardToolBar setItems: [NSArray arrayWithObjects:
                                [[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStyleBordered target:self action:@selector(previousTextField)],

                                [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStyleBordered target:self action:@selector(nextTextField)],
                                [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)],
                                nil]];
    textField.inputAccessoryView = keyboardToolBar;

}



- (void)nextTextField {


    if (textField1) {

        [textField1 resignFirstResponder];
        [textField2 becomeFirstResponder];

    }

}

-(void)previousTextField
{

    if (textField2) {
        [textField2 resignFirstResponder];
        [textField1 becomeFirstResponder];
    }


}

-(void)resignKeyboard {

    [textField1 resignFirstResponder];
    [textField2 resignFirstResponder];

}
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
user2021443
  • 209
  • 2
  • 2
  • Thanks for the idea/concept...I'll just need to make some changes. – James Laurenstin Oct 26 '13 at 00:02
  • i want to this code for 9 textfield so i do this if (textField1) { [textField1 resignFirstResponder]; [textField2 becomeFirstResponder]; } 9 times for differnt textfield but its not working why? – Ravi Ojha Mar 17 '15 at 09:45
5

I have a utility class that basically does this for you.

https://github.com/kalvish21/CustomKeyboard

The idea is very simple. You have to add an accessory tool bar with bar button items on it. There's a delegate which defines where what that button will do.

KVISH
  • 12,923
  • 17
  • 86
  • 162
4

https://github.com/hackiftekhar/IQKeyboardManager

This is the best keyboard handler I have seen so far. Very excellent way to manage Text inputs.

Some of its features 1) ZERO LINE OF CODE

2) Works Automatically

3) No More UIScrollView

4) No More Subclasses

5) No More Manual Work

6) No More #imports

i.AsifNoor
  • 587
  • 5
  • 14
  • 1
    This repo is a year out of date. Use the repo its forked from, https://github.com/hackiftekhar/IQKeyboardManager – tupakapoor Nov 20 '15 at 16:42
1

This is a custom control which is placed directly above the keyboard. I think a UIToolbar can be used for that.

Previous and next passes around the firstResponder of the textFields and Done will do the resign as well as hide the toolbar.

To match the keyboard animation have a look at this code I found or at SO: "What is the iPhone's default keyboard animation rate?"

Community
  • 1
  • 1
Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
  • Yeah I tried something similar but when I click in TextBox then Toolbar appears immediately and keyboard appears from below so it looks as seperate.it is not in sync while getting displayed. It does look seperate. I want that both keyboard and toolbar should come together from bottom of the screen. What should I do? – Parth Bhatt Apr 08 '11 at 07:26
  • Use an animation block matching the keyboad. – Nick Weaver Apr 08 '11 at 07:29
  • Thanks for the input. I am newbie into animation. How to use the animation block matching with keyboard? – Parth Bhatt Apr 08 '11 at 07:31
  • Have a look at Apple's [Animation Programming Guide for Cocoa](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/AnimationGuide/Articles/TimingAnimations.html) – Nick Weaver Apr 08 '11 at 09:06
0

I have created a repository with an implementation of this feature that works on iPhone/iPad in all orientations and highly customizable.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
0

As mentioned in other answers, it's the inputAccessoryView that you're looking for.

I would like to add an alternative way here, by using this cocoapods:

pod 'UITextField-Navigation'

All UITextFields will have two additional properties: nextTextField and previousTextField. You can then simply connect the nextTextField in the Interface Builder and the Previous, Next and Done buttons are added automatically for you, all functional, no more code is needed, no subclassing.

You can also customize the UI, add more buttons, etc as you want to.

enter image description here enter image description here

Thanh Pham
  • 2,021
  • 21
  • 30