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.
Asked
Active
Viewed 1.2k times
4 Answers
22
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

Xu Yin
- 3,932
- 1
- 26
- 46
-
And also implement the `doneWithNumberPad:` method to actually resign first responder on the text field. – rmaddy Feb 07 '14 at 18:32
-
@rmaddy i realized that, actually was editing.. since in my app it's a number pad, in this case, i changed that name to a slightly general name – Xu Yin Feb 07 '14 at 18:35
-
will this work with my UITextView as well? I see that the code refers to a UITextField? – Benny Abramovici Feb 07 '14 at 18:41
-
@BennyAbramovici yes, that will, let me edit my answer and you can try that. – Xu Yin Feb 07 '14 at 18:44
-
@BennyAbramovici ok, i changed that to text view, please try that. – Xu Yin Feb 07 '14 at 18:47
-
1This works very well and I'm using it in my own app. I would suggest changing the tint colour because the default blue may not be the best match with the black translucent bar. I just added `[doneToolbar setTintColour:[UIColor whiteColor]];` just before setting the inputAccessoryView. – kinadian Jun 19 '14 at 20:58
-
Works well and the best solution for text view no doubt! Thanks! Forget "/n" and other silly solutions or changing the return key to Done! – BootMaker Nov 13 '14 at 11:52
4
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)
}

Dania Delbani
- 816
- 1
- 11
- 27
2
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.

Tom Harrington
- 69,312
- 10
- 146
- 170
-
Thanks Tom, both ways work for retracting the keyboard. The problem is that when hitting the done button it only retracts the keyboard in the iPhone version not the iPad. on the iPad it just does nothing although the ipad keyboard can be retracted with the retract button anyways – Benny Abramovici Feb 07 '14 at 19:12
0
I know this is a too late answer but in iOS 8 i don't get hide the keyboard for my TEXT View with before answers.
Just FYI i just add this.
// Para esconder el teclado al oprimir el fondo de la pantalla
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.answerTextView endEditing:YES];
}
Extract from theapplady

Beto
- 3,438
- 5
- 30
- 37