键盘高度因ios8而异

我使用下面的代码来获得不同的iPhone 5s设备与ios8相比iPhone 4s设备与ios7的键盘高度。因此,我的文本框是移动非常高,当我点击它在IPhone5s与ios8,而相同的代码工作正常在iPhone 4s与ios7。有人指导如何在两个版本中解决问题。

- (void)keyboardWasShown:(NSNotification*)notification { NSDictionary* info = [notification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; float kbHeight; if (([UIApplication sharedApplication].statusBarOrientation== UIDeviceOrientationPortraitUpsideDown)||([UIApplication sharedApplication].statusBarOrientation== UIDeviceOrientationPortrait)) { kbHeight=kbSize.height; } else { kbHeight=kbSize.width; } UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0); self.scroll.contentInset = contentInsets; self.scroll.scrollIndicatorInsets = contentInsets; CGRect rect = self.view.frame; rect.size.height -= (kbHeight); if (!CGRectContainsPoint(rect, self.activeField.frame.origin)) { CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y - ((kbHeight) - self.activeField.frame.size.height)); [self.scroll setContentOffset:scrollPoint animated:NO]; } } 

简单地从你的代码中取代这一行

 CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

 CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

让我知道如果它的作品

这不是一个问题,你得到不同大小的键盘,因为PredictiveEnable

键盘的高度是216 ,这是固定的,但是当Predictive启用时,你将得到253高度。

在这里输入图像说明

所以你必须为这两个条件编写代码。

使用这个代码,可能会帮助你

 - (void)keyboardWillShow:(NSNotification*)note { NSDictionary* info = [note userInfo]; CGSize _kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; float kbHeight = _kbSize.width > _kbSize.height ? _kbSize.height : _kbSize.width; } 

kbHeightvariables存储键盘的高度。