75

I have created a UIToolBar programmatically and added a UITextField on it. Now, I need that toolbar to be above the keyboard when I click in another text field.

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,400, 320, 60)];
[self.view addSubview:toolBar];

UITextField *txtView=[[UITextField alloc]initWithFrame:CGRectMake(0, 400, 260, 30)];
txtView.backgroundColor =[UIColor  grayColor];
txtView.placeholder=@"Address";
UIBarButtonItem *txtfieldItem=[[UIBarButtonItem alloc]initWithCustomView:txtView];
toolBar.items =[NSArray arrayWithObject:txtfieldItem];
rmaddy
  • 298,130
  • 40
  • 468
  • 517
Sushil Sharma
  • 927
  • 1
  • 7
  • 9
  • 1
    you already have the answer here. plz check into it... http://stackoverflow.com/a/10594891/3615320 – Chan May 28 '14 at 07:11
  • possible duplicate of [How to get keyboard with Next, Previous and Done Button?](http://stackoverflow.com/questions/5591792/how-to-get-keyboard-with-next-previous-and-done-button) – Leena May 28 '14 at 08:31
  • 1
    textField.inputAccessoryView = yourToolBar; – Vineesh TP May 28 '14 at 08:50
  • There is a newer way to do it than using a UIToolBar, http://pinkstone.co.uk/how-to-add-your-own-shortcuts-above-the-keyboard-in-ios-9/ – neoneye Jul 04 '17 at 10:29

9 Answers9

132
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
                               [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                               nil];
[numberToolbar sizeToFit];
phonenumberTextField.inputAccessoryView = numberToolbar;

To Dismiss Keyboard:

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

Swift 3:

let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 50))
numberToolbar.barStyle = UIBarStyle.Default
numberToolbar.items = [
            UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelNumberPad"),
            UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
            UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")]
    numberToolbar.sizeToFit()
    phonenumberTextField.inputAccessoryView = numberToolbar

Swift 4.2:

let numberToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
numberToolbar.barStyle = .default
numberToolbar.items = [
UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelNumberPad)),
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneWithNumberPad))]
numberToolbar.sizeToFit()
phonenumberTextField.inputAccessoryView = numberToolbar

...

@objc func cancelNumberPad() {
    //Cancel with number pad
}
@objc func doneWithNumberPad() {
    //Done with number pad
}
achi
  • 4,386
  • 2
  • 22
  • 36
Kittu
  • 1,565
  • 1
  • 9
  • 10
  • 2
    Or it can be done using storyboard like [here](http://stackoverflow.com/a/41546987/1151916) – Ramis Jan 09 '17 at 11:28
  • 4
    you can add this to all textfields by doing `UITextField.appearance().inputAccessoryView = /* your tool bar setup code */` – Charlton Provatas Mar 15 '18 at 15:14
  • in swift 4 selector should be like this: UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.OnTapDone(_:))) – Mashhadi Apr 11 '18 at 19:26
  • Toolbar width should be set based on screen-size not view-size, editing that in now along with a method to dismiss keyboard – Albert Renshaw Jun 20 '18 at 16:36
101

You do not need to do this in code anymore.

  1. Just simply drag UIView to the top bar of current scene and customize it as you want.

enter image description here

  1. In code simply put IBOutlet for both: toolbarView and textView and make connections.

    @IBOutlet private var toolbarView: UIView!
    @IBOutlet private var textView: UITextView!
    
  2. In viewDidLoad setup your toolbarView as accessoryView of your UItextView.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        textView.inputAccessoryView = toolbarView
    }
    

The result is following:

enter image description here enter image description here

Bartłomiej Semańczyk
  • 52,820
  • 43
  • 206
  • 318
21

For swift (1.2):

let numberToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
numberToolbar.barStyle = UIBarStyle.Default

numberToolbar.items = [
    UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardCancelButtonTapped:"),
    UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
    UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "keyboardDoneButtonTapped:")]

numberToolbar.sizeToFit()
yourTextView.inputAccessoryView = numberToolbar
Sunkas
  • 8,816
  • 5
  • 55
  • 94
9

You Can Use this code it work for me.

-(void)viewdidload
{
 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]];
  textField.inputAccessoryView = keyboardDoneButtonView;
 }
-(void)doneClicked:(id)sender
{
NSLog(@"Done Clicked.");
[self.view endEditing:YES];
} 
Ravi Ojha
  • 1,569
  • 15
  • 25
Anil Prasad
  • 701
  • 1
  • 6
  • 16
7

You can use the UITextFields inputAccessoryView property

    txtField.inputAccessoryView = toolBar;
Shujito
  • 3,023
  • 2
  • 15
  • 17
Anil Varghese
  • 41,329
  • 9
  • 90
  • 110
4

Swift 3

    let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
    toolBar.barStyle = UIBarStyle.default
    toolBar.items = [
        UIBarButtonItem(title: "Button1", style: UIBarButtonItemStyle.plain, target: self, action: #selector(test2)),
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil),
        UIBarButtonItem(title: "Button2", style: UIBarButtonItemStyle.plain, target: self, action: #selector(test1))]
    toolBar.sizeToFit()

    myTextField.inputAccessoryView = toolBar
Adrian
  • 16,820
  • 32
  • 99
  • 188
fs_tigre
  • 9,459
  • 12
  • 53
  • 111
1
textField.inputAccessoryView=[weakSelf addToolBar];
[textField setKeyboardType:UIKeyboardTypeNumberPad];

and add a method

-(UIToolbar *)addToolBar
{

    UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithTitle:@"DONE" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];
    UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    NSArray* toolBarItems=[[NSArray alloc]initWithObjects:done, nil];
    [toolBar setItems:toolBarItems];
    return toolBar;
}
DURGESH
  • 1,585
  • 12
  • 13
0

Swift 5.0 and above

           let toolBar = UIToolbar(frame: CGRect(x: 0.0,
                                                  y: 0.0,
                                                  width: UIScreen.main.bounds.size.width,
                                                  height: 44.0))//1
            let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)//2
            let DoneButton = UIBarButtonItem(title: "Done", style: .plain, target: target, action: #selector(tapDone))//3
            toolBar.setItems([flexibleSpace, DoneButton], animated: false)
           textField.inputAccessoryView = toolBar
       // Done Action
        @objc func tapDone() {
           self.view.endEditing(true)
         }
Gurjinder Singh
  • 5,576
  • 44
  • 41
-1

In Swift 3 and 4

 let toolBar = UIToolbar()
 toolBar.barStyle = UIBarStyle.default
 toolBar.isTranslucent = true
 toolBar.isUserInteractionEnabled = true
 toolBar.sizeToFit()
 toolBar.items = [
        UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.sendCodeBtnAction(sender:)))]

    tfPhone.inputAccessoryView = toolBar
Spydy
  • 2,147
  • 17
  • 14