How to get keyboard with Next, Previous and Done Button?
Asked Answered
P

8

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?

Postman answered 8/4, 2011 at 7:20 Comment(0)
P
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.

Prolific answered 8/4, 2011 at 7:31 Comment(1)
Or it can be done using storyboard like hereSporophyll
B
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.

Blubber answered 16/1, 2012 at 14:50 Comment(1)
Just now discovering BSkeyboardControls and love it! Need it to work with a UIDatePicker instead of a textfield. Any sample code for this?Electrocautery
V
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];

}
Vacillation answered 27/9, 2013 at 14:52 Comment(2)
Thanks for the idea/concept...I'll just need to make some changes.Hartzke
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?Decani
R
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.

Reliquiae answered 10/10, 2012 at 16:10 Comment(0)
G
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

Glow answered 21/12, 2014 at 15:35 Comment(1)
This repo is a year out of date. Use the repo its forked from, github.com/hackiftekhar/IQKeyboardManagerDesiccant
H
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?"

Harald answered 8/4, 2011 at 7:23 Comment(4)
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?Postman
Use an animation block matching the keyboad.Harald
Thanks for the input. I am newbie into animation. How to use the animation block matching with keyboard?Postman
Have a look at Apple's Animation Programming Guide for CocoaHarald
C
0

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

Caution answered 28/8, 2012 at 11:26 Comment(0)
E
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

Explanation answered 28/6, 2016 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.