keyboard height varies in ios8

2019-05-19 07:09发布

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

3条回答
We Are One
2楼-- · 2019-05-19 07:41

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.

enter image description here

So you have to write code for both conditions.

查看更多
ら.Afraid
3楼-- · 2019-05-19 07:46

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

查看更多
相关推荐>>
4楼-- · 2019-05-19 07:47

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.

查看更多
登录 后发表回答