Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Virtual Keyboard of UITextView when 'Done' Presses

I want to hide (resignFirstResponder) the virtual keyboard of UITextView when 'Done' presses. Theres no 'Did End on Exit' in UITextView. In UITextField i connect the 'Did End on Exit' with an IBAction and call resignFirstResponder method. How can i do this with UITextView?

like image 808
Taimur Ajmal Avatar asked Mar 03 '11 00:03

Taimur Ajmal


2 Answers

The correct way to handle this is to add a done button in an inputAccessoryView to the UITextView. The inputAccessoryView is the bar that sometimes appears above the keyboard.

In order to implement the inputAccessoryView simply add this method (or a variation thereof) and call it in viewDidLoad.

- (void)addInputAccessoryViewForTextView:(UITextView *)textView{

//Create the toolbar for the inputAccessoryView
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[toolbar sizeToFit];
toolbar.barStyle = UIBarStyleBlackTranslucent;

//Add the done button and set its target:action: to call the method returnTextView:
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                       [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(returnTextView:)],
                       nil];

//Set the inputAccessoryView
[textView setInputAccessoryView:toolbar];

}

Then handel the button being pressed by implementing the action method you called with resignFirstResponder.

- (void) returnBreakdown:(UIButton *)sender{

[self.textView resignFirstResponder];

}

This should result in a working "Done" button appearing in a standard toolbar above the keyboard.

like image 186
iward1 Avatar answered Sep 25 '22 06:09

iward1


I'm assuming by the "Done" button you mean the return key. It's not as intuitive as you might think. This question covers it pretty well.

like image 21
Matt Wilding Avatar answered Sep 25 '22 06:09

Matt Wilding