How to add done button on keyboard on top of keyboard in IOS?

Hope this help :)

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 action:@selector(yourTextViewDoneButtonPressed)];
keyboardToolbar.items = @[flexBarButton, doneBarButton];
self.yourTextView.inputAccessoryView = keyboardToolbar;

and then add yourTextViewDoneButtonPressed method

-(void)yourTextViewDoneButtonPressed
{
    [self.yourTextView resignFirstResponder];
}

It can be done using storyboard:

  1. Add UITextField to UIViewController view.
  2. Add UIToolbar in the first level of UIViewController
  3. Add UIBarButtonItem into the UIToolbar.
  4. Connect UItoolbar to the code using IBOutlet.
  5. Connect UIBarButtonItem to the code using IBAction (as didClick).
  6. Make the UITextField will be delegating to UIViewController.
  7. In the didClick function end editing (view.endEditing(true))
  8. In the delegate function textFieldShouldBeginEditing should be: textField.inputAccessoryView = toolbar and returns true.

enter image description here


Swift 3:

func setDoneOnKeyboard() {
    let keyboardToolbar = UIToolbar()
    keyboardToolbar.sizeToFit()
    let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(InsertStatusVC.dismissKeyboard))
    keyboardToolbar.items = [flexBarButton, doneBarButton]
    self.fullNameTextField.inputAccessoryView = keyboardToolbar
}

@objc func dismissKeyboard() {
    view.endEditing(true)
}