Hide Virtual Keyboard of UITextView when 'Done' Presses
Asked Answered
E

5

5

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?

Everson answered 3/3, 2011 at 0:13 Comment(0)
K
2

Here is the Swift version of the accessory "Done" 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()
}
Kinna answered 9/12, 2015 at 5:10 Comment(0)
E
7

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.

Eye answered 31/7, 2013 at 16:14 Comment(0)
C
4

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.

Cadency answered 3/3, 2011 at 0:57 Comment(0)
D
3

you could add this to an action if you want to be able to use your return key [[self view] endEditing: YES];

Dander answered 25/3, 2013 at 16:32 Comment(1)
I like this solution. The equivalent in Xamarin.IOS is View.EndEditing(true);Replay
M
3

Make sure you declare support for the UITextViewDelegate protocol.

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

In .m file, implement below method

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES; }
Medeah answered 2/8, 2013 at 10:14 Comment(0)
K
2

Here is the Swift version of the accessory "Done" 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()
}
Kinna answered 9/12, 2015 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.