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

问题描述 投票:4回答:2

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

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

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

可能是什么原因?

ios iphone uikeyboard
2个回答
7
投票

我找到了答案。

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

当您的常量值为0(键盘隐藏)时,您的视图仍然是0以上49点。

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


5
投票

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

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

Objective-C的::

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

迅速:

let tabBarHeight = tabBarController?.tabBar.frame.height ?? 0
let keyboardFrame = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue ?? 0.0
let adjustedKeyboardHeight = keyboardFrame.height - tabBarHeight
© www.soinside.com 2019 - 2024. All rights reserved.