Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIKeyboard Suggestions height for iOS 8 notification?

I'm trying to keep a text field sitting atop the keyboard in iOS 8. But when a user swipes up or down the top of the keyboard to show or dismiss iOS 8 word suggestions, I need to a notification of the new height of the keyboard so I can move my text field up or down by that height.

How can I accomplish this?

Thanks!

like image 643
Tony Friz Avatar asked May 02 '15 02:05

Tony Friz


2 Answers

You can register for UIKeyboardDidShowNotification and then get the keyboard frame from the notification with UIKeyboardFrameEndUserInfoKey.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(handleKeyboardDidShowNotification:) 
                                             name:UIKeyboardDidShowNotification 
                                           object:nil];


- (void)handleKeyboardDidShowNotification:(NSNotification *)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    // Move your textview into view here
}

This notification will be sent even in the event that the keyboard is already showing and is just going to change size, so you'll get it whenever you swipe up or down on the top of the keyboard.

like image 147
charmingToad Avatar answered Oct 16 '22 19:10

charmingToad


Here's some code from a custom inputView I built. It even handles the animation curve for your custom view so that it matches the velocity of the keyboard and moves along with it.

The notification will fire when suggestions are shown/hidden.

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification object:nil]; }

- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary* info = [notification userInfo];
    NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];
    CGSize endKbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    CGRect baseFrame = self.frame;
    baseFrame.origin.y = CGRectGetMaxY(self.frame) - (endKbSize.height - 5);

    [UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
        self.frame = baseFrame;
    } completion:^(BOOL finished) {
        self.frame = baseFrame;
    }]; }

- (void)keyboardWillHide:(NSNotification *)notification {

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

    [UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
        self.frame = _originalRect;
    } completion:nil]; 
}
like image 32
Beau Nouvelle Avatar answered Oct 16 '22 19:10

Beau Nouvelle