How to add ToolBar Above Keyboard?

I have created a toolBar programmatically and added a UITextField on it. Now, I need that ToolBar to be above keyboard when clicked in another textfield.

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];

  • Text field for currency Swift
  • How to detect delete key on an UITextField in iOS 8?
  • Format UITextField text without having cursor move to the end
  • How to disable copy paste option from UITextField programmatically
  • Prevent default keyboard from showing when UITextField is pressed
  • UIPickerView as inputView of UITextField
  • 7 Solutions Collect From Internet About “How to add ToolBar Above Keyboard?”

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.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;
    

    Using Swift:

    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: "cancelNumberPad"),
                UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
                UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneWithNumberPad")]
        numberToolbar.sizeToFit()
        phonenumberTextField.inputAccessoryView = numberToolbar
    

    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

    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
    

    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];
    } 
    

    You can use the UITextFields inputAccessaryView property

        txtField.inputAccessoryView = toolBar;
    

    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
    
    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;
    }