Add a done button to the top of the keyboard

12,212

Solution 1

Please try this code

 //set up a placeholder variable for the textfield user typing
 UITextView *currentTextView;

-(void)addDoneToolBarToKeyboard:(UITextView *)textView
{
    UIToolbar* doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    doneToolbar.barStyle = UIBarStyleBlackTranslucent;
    doneToolbar.items = [NSArray arrayWithObjects:
                       [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonClickedDismissKeyboard)],
                       nil];
    [doneToolbar sizeToFit];
    textView.inputAccessoryView = doneToolbar;
}

 //remember to set your text view delegate
 //but if you only have 1 text view in your view controller
 //you can simply change currentTextField to the name of your text view
 //and ignore this textViewDidBeginEditing delegate method
 - (void)textViewDidBeginEditing:(UITextView *)textView
 {
     currentTextView = textView;
 }

-(void)doneButtonClickedDismissKeyboard
{
     [currentTextView resignFirstResponder];
}

and add this in your view did load

 [self addDoneToolBarToKeyboard:self.textView];

Hope that helps

Solution 2

same answer swift3 :

func addDoneToolBarToKeyboard(textView:UITextView)
{
    let doneToolbar : UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
    doneToolbar.barStyle = UIBarStyle.default
    let flexibelSpaceItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)

    let hideKeyboardItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.dismissKeyboard))
    doneToolbar.items = [flexibelSpaceItem, hideKeyboardItem!]
    doneToolbar.sizeToFit()
    textView.inputAccessoryView = doneToolbar
}

and dismiss function will be:

func dismissKeyboard()
{
  self.view.endEditing(true)
}

Solution 3

You can do it in one line of code at any time by doing this:

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

That sends resignFirstResponder up the responder chain. The current text view will be first responder, so it will get the message and resign first responder.

Share:
12,212
Benny Abramovici
Author by

Benny Abramovici

I need all the help I can get

Updated on July 02, 2022

Comments

  • Benny Abramovici
    Benny Abramovici almost 2 years

    I am making a universal app that has a UITextView. When the app run on the iPad there is a button on the lower right which enables me to dismiss the keyboard. The iPhone version does not have such a button. I have seen on some iPhone apps a bar on top of the keyboard that has a done option. Is there an easy way to add an iPad style dismiss keyboard button to the iPhone app as well. If not, what is the best way to add a done style bar to the top of the keyboard? Thanks in advance.