iOS UITableView带输入框时与搜狗输入法做自适应键盘
系统输入法自适应键盘高度
博主最近在做输入(UITextView,UITextField)的时候,总是遇到键盘弹出后,遮盖住输入框情况,后来监听了UIKeyboardWillShowNotification之后,得到键盘的高度,并且让输入框或者UITableView(输入框在UITableView或者UIScrollView上)向上提高一段距离就能让键盘遮盖不住输入框.
实现起来很简单.
在viewController中添加通知,然后读出各种键盘信息做操作.
// 键盘将要显示时
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
// 键盘将要隐藏时
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
- (void)keyboardWillShow:(NSNotification*)notification{
NSLog:("%@", notification);
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
_keyBoardHeight = keyboardRect.size.height;
//记录当前的位置,在键盘消失之后恢复当前的位置
self.historyPoint = self.tableView.contentOffset;
//位置可根据自己的实际情况来计算
self.tableView.contentOffset = CGPointMake(0, self.tableView.contentSize.height-(KScreenHeight-toolBarHeight)+_keyBoardHeight);
}
- (void)keyboardWillHide:(NSNotification*)notification{
//键盘消失,恢复原来的位置
self.tableView.contentOffset = self.historyPoint;
}
在keyboardWillShow:中可监听到如下数据:
NSConcreteNotification 0x158085400 {name = UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";//动画时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";//键盘的Bounds
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";//Keyboard弹出之后最后的值,一般用这个取键盘的高
UIKeyboardIsLocalUserInfoKey = 1;
}}
但是这种方法在搜狗输入法上就不起作用了.
搜狗输入法自适应键盘高度
博主经过一番研究发现,搜狗输入法键盘弹出时,会发送三次UIKeyboardWillShowNotification.而系统输入法只发送一次.
三次收到的通知userInfo如下
搜狗输入法发出的通知第一次
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 0}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 667}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 667}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 0}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 667}, {375, 0}}";
UIKeyboardIsLocalUserInfoKey = 1;
第二次
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 667}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 559}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 0}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
UIKeyboardIsLocalUserInfoKey = 1;
第三次
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 282}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 559}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 526}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 451}, {375, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 385}, {375, 282}}";
UIKeyboardIsLocalUserInfoKey = 1;
系统输入法发送的通知
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 283.5}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 808.75}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 525.25}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 283.5}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 383.5}, {375, 283.5}}";
UIKeyboardIsLocalUserInfoKey = 1;
比较它们的差异,搜狗输入法的UIKeyboardFrameBeginUserInfoKey在第一次和第二次时,frame.size.height都是0;
而第三次的才算正常的,和系统发出的一样.
因此添加判断
- (void)keyboardWillShow:(NSNotification*)notification{
NSLog:("%@", notification);
NSDictionary *userInfo = [paramNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
_keyBoardHeight = keyboardRect.size.height;
CGRect beginUserInfo = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
if (beginUserInfo.size.height <=0) {//!!搜狗输入法弹出时会发出三次UIKeyboardWillShowNotification的通知,和官方输入法相比,有效的一次为dUIKeyboardFrameBeginUserInfoKey.size.height都大于零时.
return;
}
//记录当前的位置,在键盘消失之后恢复当前的位置
self.historyPoint = self.tableView.contentOffset;
//位置可根据自己的实际情况来计算
self.tableView.contentOffset = CGPointMake(0, self.tableView.contentSize.height-(KScreenHeight-toolBarHeight)+_keyBoardHeight);
}
解决问题.