UIKeyboard Suggestions for height for iOS 8 notification?

I am trying to keep a textbox sitting on the keyboard in iOS 8. But when the user swipes up or down the top of the keyboard to show or reject the iOS 8 word suggestions, I need to be notified of the new keyboard height so that I can move the textbox up or down this height.

How can i do this?

Thank!

+3


source to share


2 answers


You can register for UIKeyboardDidShowNotification and then get the keyboard frame from the notification using 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 if the keyboard is already displayed and just resizes, so you'll get it whenever you swipe up or down at the top of the keyboard.

+7


source


Here is some code from a custom inputView that I have built. It even handles the animation curve for your custom view to match the speed of the keyboard and move with it.

The notification will be triggered when showing / hiding offers.



- (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]; 
}

      

+1


source







All Articles