Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIKeyboardFrameEndUserInfoKey height incorrect (should be 48pt less)

I am trying to scroll my view according to keyboard height. Here is my code in viewDidLoad:

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

(after I obtain the lastKeyboardFrame I use it to push my views top etc.)

I have some text views and my view controller is their delegate. Here is how I animate the whole view up:

-(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 is the bottom constraint of my view to bottom layout guide. It works, but the keyboard height is incorrectly displayed. Here is how it displays:

enter image description here

After some trial and error, I've found out that 'extra' space is exactly 48pt in height. If I subtract 48 from the height, it works well:

enter image description here

Tested both in iOS 7 iPhone 4s simulator and iPhone 6 Plus, it's the same regardless of screen size. The first thing I've considered was predictive input bar at the top, but then I've realized that the problem is also persistent on iOS 7.1 too, and that my keyboard (Turkish) doesn't even have that bar available.

What could be the reason?

like image 655
Can Poyrazoğlu Avatar asked Feb 01 '15 15:02

Can Poyrazoğlu


1 Answers

For anyone still stumbling on this: I think the safest answer is buried in the comments of @Oded's answer:

You should get the height of the UITabBar then subtract it from the keyboard height:

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
like image 164
toddg Avatar answered Oct 16 '22 23:10

toddg