键盘高度因ios8而异 [英] keyboard height varies in ios8

查看:95
本文介绍了键盘高度因ios8而异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码获取iPhone is设备与ios8相比的键盘高度与使用ios7的IPhone4s设备相比有所不同。因此,当我使用ios8在IPhone5s中点击时,我的文本字段移动得非常高,而相同的代码可以工作在iphone7的iPhone 4s中很好。可以指导如何在两个版本中解决问题。

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


推荐答案

简单替换代码中的行

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

使用

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

让我知道它是否有效

这篇关于键盘高度因ios8而异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆