5
votes

I see numerous posts (e.g. "hide keyboard for text field in swift programming language") about how to make the keyboard disappear after hitting the return button, or by clicking outside the UITextView (and subsequently resigning first responder, and/or setting endEditing to true). This is not what I want to do; I neither want to use the return key -- I want the user to be able to use the return key for actually adding a newline to the text they're entering in the UITextView -- nor force the user to click outside in order to dismiss the keyboard.

On an iPad there is a "dismiss keyboard" button which naturally appears, as part of the keyboard itself, that dismisses the keyboard. It's usually in the lower right of the keyboard (below the right shift key), and it's a little picture of a keyboard with a down arrow on it.

How does one enable this button on an iPhone, i.e. to have the keyboard which appears also include the dismiss button?

This post: "dismiss iphone UITextView Keyboard" seems to advocate adding a toolbar to the top of the keyboard. Is there not a way to simply select an optional keyboard that includes a dismiss-key option, similar to the way one can provide a keyboard with "@" on the main keyboard screen when an email address is expected?

Thanks.

PS- For clarity: I do not want to set the return key to "Done" (as per this post), I want to keep it as a return key. I'm willing to add an extra UIButton if that's the only way, but it seems like there should be a keyboard option which includes a dismiss button already, in addition to the return key.

2

2 Answers

4
votes

I'm sad to say that the correct answer to your request is that there is no possible way to do what you want.

As you have garnered from your research, the community has found lots of work arounds, but that's all we have.

1
votes

add a notification, which will be triggered when keyboard is about to show

Then custom a view, here you can change it to your button

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentViewPoint:) name:UIKeyboardWillShowNotification object:nil];

- (void) changeContentViewPoint:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyBoardEndY = value.CGRectValue.origin.y;  // get the y of the keyboard

    NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // add animation, make your view move as the keyboard moves
    [UIView animateWithDuration:duration.doubleValue animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:[curve intValue]];

        _mainView.center = CGPointMake(_mainView.center.x, keyBoardEndY - STATUS_BAR_HEIGHT - _mainView.bounds.size.height/2.0);   // keyBoardEndY including the height of the status bar, so get rid of this height. 

    }];



}