Swift: enable “cancel keyboard” button on iPhone?

I see numerous messages (for example, "hide the keyboard for a text field in fast programming language" ) about how to make the keyboard disappear after pressing the return button or clicking outside of UITextView (and subsequently canceling the first responder and / or setting endEditing to true). This is not what I want to do; I don’t want to use the return key — I want the user to be able to use the return key to actually add a new line to the text that they enter in the UITextView — and not force the user to click on it to remove the keyboard.

The iPad has a “remove keyboard" button, which naturally appears as part of the keyboard itself that rejects the keyboard. It is usually located in the lower right corner of the keyboard (under the right shift key), and this is a small image of the keyboard with a down arrow on it.

How to enable this button on iPhone, i.e. turn on the keyboard, which also includes a dismissal button?

This post: “fire iphone UITextView Keyboard” seems to advocate adding a toolbar to the top of the keyboard. Is it not possible to simply select an additional keyboard that includes the “Disable key” option, similar to how you can expect the keyboard with “@” on the main screen of the keyboard when an email address is expected?

Thanks.

PS - For clarity: I do not want the return key to be "Done" (according to this post ), I want to save it as a return key. I am ready to add an additional UIButton if this is the only way, but it seems that in addition to the return key there should be a keyboard option that includes a dismiss button.

+4
source share
2 answers

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

As you received from your research, the community has found a lot of work, but all that we have.

+4
source

add a notification that will be triggered when the keyboard is about to show

,

[[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. 

    }];



}
+1

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


All Articles