ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)

背景:

  ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度(包括米聊也是这么做的)。

  可是在ios5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。

解决方法:

  其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”

  处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)

  1)在要使用键盘的视图控制器中,接收键盘事件的通知:

Objective-c代码 复制代码 收藏代码
  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
  2.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
  3.  
  4.         // 键盘高度变化通知,ios5.0新增的   
  5. #ifdef __IPHONE_5_0 
  6.         float version = [[[UIDevice currentDevice] systemVersion] floatValue]; 
  7.         if (version >= 5.0) { 
  8.             [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil]; 
  9.         } 
  10. #endif 

  2)然后添加键盘事件的处理代码:

    获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。

Objective-c代码 复制代码 收藏代码
  1. #pragma mark - 
  2. #pragma mark Responding to keyboard events 
  3. - (void)keyboardWillShow:(NSNotification *)notification { 
  4.      
  5.     /* 
  6.      Reduce the size of the text view so that it's not obscured by the keyboard. 
  7.      Animate the resize so that it's in sync with the appearance of the keyboard. 
  8.      */ 
  9.      
  10.     NSDictionary *userInfo = [notification userInfo]; 
  11.      
  12.     // Get the origin of the keyboard when it's displayed. 
  13.     NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
  14.      
  15.     // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. 
  16.     CGRect keyboardRect = [aValue CGRectValue]; 
  17.      
  18.     // Get the duration of the animation. 
  19.     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
  20.     NSTimeInterval animationDuration; 
  21.     [animationDurationValue getValue:&animationDuration]; 
  22.      
  23.     // Animate the resize of the text view's frame in sync with the keyboard's appearance. 
  24.     [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration]; 
  25.  
  26.  
  27. - (void)keyboardWillHide:(NSNotification *)notification { 
  28.      
  29.     NSDictionary* userInfo = [notification userInfo]; 
  30.      
  31.     /* 
  32.      Restore the size of the text view (fill self's view). 
  33.      Animate the resize so that it's in sync with the disappearance of the keyboard. 
  34.      */ 
  35.     NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; 
  36.     NSTimeInterval animationDuration; 
  37.     [animationDurationValue getValue:&animationDuration]; 
  38.      
  39.     [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration]; 

3)在视图控制器消除时,移除键盘事件的通知:

Objective-c代码 复制代码 收藏代码
  1. [[NSNotificationCenter defaultCenter] removeObserver:self]; 

ps:

  ios5隐藏功能分享——“字典功能(英英字典):

  在任何输入框中选中一个英文单词,此时会有选择项复制删除”...等,还有一个向右的箭头,点击这个向右的箭头后,就会出现定义选项,点击这个定义按钮即会弹出这个英语单词的英文解释。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值