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);
}

解决问题.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 先来看一下效果图: 效果就如展示的一样,弹出样式也很简单,但是做这个我花费了足足的一天时间,可能我对 textvi...
    ControlM阅读 5,363评论 6 3
  • 不知不觉,岁寒输入法的更新历史已经可以列出这么一长串来了。从中可以看出,岁寒的发展过程也是一个不断试错的过程,其中...
    临岁之寒阅读 34,461评论 1 6
  • 为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出...
    codeGlider阅读 14,819评论 39 91
  • 大秦崛起首攻楚, 军民抵抗存三户。 屈子大义谏怀王, 郢都殿堂鼓与呼! 抛却身家为社稷, 贬黜流离走江湖。 自沉殉...
    千秋笔阅读 3,008评论 1 22
  • 今早不如昨早 阴暗潮湿 今早胜过昨早 清爽微凉 今早不如昨早 气氛低沉 今早胜过昨早 遮天蔽日 今早不如昨早 阳光...
    Mark_mark阅读 1,384评论 0 1