在UIKeyboardWillShowNotification /上,iOS键盘高度返回不正确

Can*_*ğlu 13 ios ios8

我正在尝试在键盘出现时滚动文本视图.我试图通过https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html关注Apple自己的教程.我使用autolayout而不是滚动视图和框架,但总体思路是相同的(订阅键盘通知,并相应地设置布局约束).但是,当我显示键盘时,文本视图和键盘之间存在间隙:(这是来自土耳其语键盘的屏幕截图,但是所有安装的键盘中都存在标有红色的相同间隙,包括自定义键盘).(iPhone 6 Plus的屏幕截图,但问题也出现在其他屏幕尺寸上)

在此输入图像描述

我已经看到在iOS8中无法获得键盘高度的正确值,我尝试过使用两者UIKeyboardFrameEndUserInfoKey,UIKeyboardFrameBeginUserInfoKey但它们都会产生相同的差距.我试过安装UIKeyboardWillShowNotificationUIKeyboardDidShowNotification,但仍,我得到了同样的差距.我认为差距与iOS 8上某些键盘上的建议有关,但是当建议不存在时,它不应该报告键盘大小和建议.

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self registerForKeyboardNotifications];
}

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

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

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGRect kb = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    float height = kb.size.height;
    sendZoneBottomConstraint.constant = height;
    [UIView animateWithDuration:.2 animations:^{
        [self.view layoutIfNeeded];
    }];
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    sendZoneBottomConstraint.constant = 0;
    [UIView animateWithDuration:.2 animations:^{
        [self.view layoutIfNeeded];
    }];
}
Run Code Online (Sandbox Code Playgroud)

sendZoneBottomConstraint 是最底部视图与底部布局指南的底部间距.

更新:我已经尝试将键盘矩形从窗口坐标更改为我的视图坐标,但它没有改变任何东西.这是我尝试过的:

CGRect kb = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
kb = [self.view.window convertRect:kb toView:self.view];
float height = kb.size.height;
Run Code Online (Sandbox Code Playgroud)

更新,第2卷:我也看到iOS 8方向改变:键盘框架无法正确显示但我在iPhone 5s iOS 7.1模拟器上也遇到了问题,所以它不是iOS 8唯一的问题.我怎么解决这个问题?我绝对不想在iOS 8世界中硬编码任何键盘高度值.

更新,第3卷:我也尝试过英语键盘以及开启和关闭的建议.它仍然出现在键盘的总大小上,因此它与建议/自动完成视图无关:

在此输入图像描述 在此输入图像描述

Ban*_*Dao 18

也许您正在使用标签栏控制器,如果是,那么当您在显示键盘后边缘视图时,计算边距底部没有标签栏高度.

您可以使用以下方法找到标签栏高度:

self.tabBarController?.tabBar.frame.height
Run Code Online (Sandbox Code Playgroud)