Hide the UITextView virtual keyboard when you click Finish

I want to hide ( resignFirstResponder ) the resignFirstResponder virtual keyboard when the Finish button is clicked. In "t21" there is no "Finished at the exit." In UITextField connect 'Did End on Exit' using the IBAction method and call resignFirstResponder . How can I do this using a UITextView ?

+4
source share
5 answers

Here is a quick version of the Finish button:

 @IBOutlet weak var textView: UITextView! // In viewDidLoad() let toolbar = UIToolbar() toolbar.bounds = CGRectMake(0, 0, 320, 50) toolbar.sizeToFit() toolbar.barStyle = UIBarStyle.Default toolbar.items = [ UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil, action: "handleDone:") ] self.textView.inputAccessoryView = toolbar // ----------------- func handleDone(sender:UIButton) { self.textView.resignFirstResponder() } 
+2
source

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

To implement inputAccessoryView , simply add this method (or its variant) 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 cancel the button click by executing the action method called with resignFirstResponder .

 - (void) returnBreakdown:(UIButton *)sender{ [self.textView resignFirstResponder]; } 

This will cause the β€œFinish” button to appear on the standard toolbar above the keyboard.

+7
source

I assume that the Finish button means a return key. This is not as clear as you think. This question covers it pretty well.

+4
source

you can add this to the action if you want to use your return key [[self view] endEditing: YES];

+3
source

Make sure you declare support for the UITextViewDelegate protocol.

@interface ...ViewController : UIViewController `in the .h file.

In the .m file, implement the method below

 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; } 
+3
source

Source: https://habr.com/ru/post/1342060/


All Articles