UIKeyboardFrameEndUserInfoKey 高度不正确(应该少 48pt)

UIKeyboardFrameEndUserInfoKey height incorrect (should be 48pt less)

我正在尝试根据键盘高度滚动我的视图。这是我在 viewDidLoad:

中的代码
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillChangeFrameNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
        lastKeyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    }];

(在我获得 lastKeyboardFrame 后,我用它来推高我的观点等)

我有一些文本视图,我的视图控制器是它们的委托。以下是我如何为整个视图设置动画:

-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.editingViewBottomConstraint.constant = lastKeyboardFrame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    self.editingViewBottomConstraint.constant = 0;
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
    lastKeyboardFrame = CGRectZero;
}

self.editingViewBottomConstraint 是我对底部布局指南的视图的底部约束。它可以工作,但键盘高度显示不正确。它的显示方式如下:

经过反复试验,我发现 'extra' space 的高度恰好是 48pt。如果我从高度中减去 48,效果很好:

在iOS 7 iPhone 4s 模拟器和iPhone 6 Plus 中都进行了测试,无论屏幕大小如何,都是一样的。我首先考虑的是顶部的预测输入栏,但后来我意识到这个问题在 iOS 7.1 上也仍然存在,而且我的键盘(土耳其语)甚至没有那个栏可用。

可能是什么原因?

我找到了答案。

我很确定您有标签栏。标签栏高度为 49 点。

当您的常量值为 0(隐藏键盘)时,您的视图仍比 0 高 49 点。

所以你有两个选择: 1、不断减去49分。 2. 呈现视图控制器而不是按下以摆脱标签栏。

对于仍然在这个问题上绊脚石的任何人:我认为最安全的答案隐藏在@Oded 的回答的评论中:

您应该得到 UITabBar 的高度,然后从键盘高度中减去它:

Objective-C::

[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height-self.tabBarController.tabBar.frame.‌​size.height

Swift:

let tabBarHeight = tabBarController?.tabBar.frame.height ?? 0
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? 0.0
let adjustedKeyboardHeight = keyboardFrame.height - tabBarHeight