keyboard height varies in ios8

2019-05-19 07:37发布

问题:

Am using the below code to get keyboard height which varies in IPhone 5s device with ios8 compared to IPhone4s device with ios7 .As a result my textfield is moving very high when i tap on it in IPhone5s with ios8 while the same code works fine in IPhone 4s with ios7.Can someone guide how the problem can be fixed in both versions.

- (void)keyboardWasShown:(NSNotification*)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    float kbHeight;
    if (([UIApplication sharedApplication].statusBarOrientation== UIDeviceOrientationPortraitUpsideDown)||([UIApplication sharedApplication].statusBarOrientation== UIDeviceOrientationPortrait))
    {
        kbHeight=kbSize.height;
    }
    else
    {
        kbHeight=kbSize.width;
    }



    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);

    self.scroll.contentInset = contentInsets;
    self.scroll.scrollIndicatorInsets = contentInsets;

    CGRect rect = self.view.frame;
    rect.size.height -= (kbHeight);


    if (!CGRectContainsPoint(rect, self.activeField.frame.origin))
    {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y - ((kbHeight) - self.activeField.frame.size.height));
        [self.scroll setContentOffset:scrollPoint animated:NO];
    }
}

回答1:

Simply Replace the line from your code

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

With

CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

Let me know if it works



回答2:

This is not an issue, you are getting different size keyboard because Predictive is Enable.

The height of keyboard is 216 which is fixed but when Predictive is Enabled you will get 253 as height.

So you have to write code for both conditions.



回答3:

use this code, may help you

- (void)keyboardWillShow:(NSNotification*)note {
      NSDictionary* info = [note userInfo];
      CGSize _kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
      float kbHeight = _kbSize.width > _kbSize.height ? _kbSize.height : _kbSize.width;
}

kbHeight variable stored height of the keyboard.