Dismiss keyboard for UITextView without using Retu

2019-07-04 22:56发布

I'm using a UITextView and want to keep the normal usage of Return key, i.e. to insert a new line. But how do I dismiss the keyboard when I can't use the Return key for that?

5条回答
姐就是有狂的资本
2楼-- · 2019-07-04 23:17

How the user triggers it is a design decision: another button, a swipe gesture?

When it's triggered, call:

[self.textView resignFirstResponder];
查看更多
爷、活的狠高调
3楼-- · 2019-07-04 23:20

A lot of people add a UIToolbar with a Done button on it above the keyboard. This is how the Safari app does it as well and in my opinion it is the best way to handle this situation. See a pic here.

To dismiss the keyboard, you just have to do [textField resignFirstResponder];.

Here is an okay example of how to add the UIToolbar when the keyboard shows/hides.

查看更多
We Are One
4楼-- · 2019-07-04 23:29

Make sure you declare support for the UITextViewDelegate protocol.

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:     (NSString *)text

{

    if([text isEqualToString:@"\n"]) 
    {
        [textView resignFirstResponder];

        return NO;
    }

    return YES;

}

Enjoy Buddy with this code....

查看更多
冷血范
5楼-- · 2019-07-04 23:31

You could just use a background tap to hide the keyboard.

查看更多
\"骚年 ilove
6楼-- · 2019-07-04 23:36

A good way to Dismiss keyboard.

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    //NSLog(@"ShouldBeginEditing");

    if(textView == indexOneTW)
    {
          here use uibutton and set its target to a method which contains 
          [urTextView resignFirstResponder] 
    }

    return TRUE;
}
查看更多
登录 后发表回答