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

时间:2015-02-01 15:47:29

标签: ios iphone uikeyboard

我正在尝试根据键盘高度滚动我的视图。这是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是我对底部布局指南的视图的底部约束。它工作正常,但键盘高度显示不正确。以下是它的显示方式:

enter image description here

经过一些反复试验,我发现了'额外的'空间正好是48pt的高度。如果我从高度减去48,它运作良好:

enter image description here

在iOS 7 iPhone 4s模拟器和iPhone 6 Plus中都进行了测试,无论屏幕大小如何,它都是相同的。我首先考虑的是顶部的预测输入栏,但后来我意识到问题在iOS 7.1上也是持久性的,我的键盘(土耳其语)甚至没有那个酒吧。

可能是什么原因?

2 个答案:

答案 0 :(得分:7)

我找到了答案。

我很确定你有一个标签栏。标签栏高度为49点。

当你的常数值为0(键盘隐藏)时,你的视图仍然是高于0的49点。

所以你有两个选择: 1.减去49分。 2.呈现视图控制器而不是推动去掉标签栏。

答案 1 :(得分:5)

对于仍然磕磕绊绊的人:我认为最安全的答案都隐藏在@ Oded的回答中:

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

<强>目标-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