现在的位置: 首页 > 综合 > 正文

完美解决ios4与ios5输入框随键盘移动问题

2017年11月30日 ⁄ 综合 ⁄ 共 3072字 ⁄ 字号 评论关闭

      iOS5中当键盘输入法切换到中文时,键盘高度由216增加到252像素。这一变化将遮住输入框。如何才能解决这一问题呢?

      在iOS5中,新增有notification(UIKeyboardWillChangeFrameNotification)可以用来监测键盘frame的变化。在iOS4中,可以通过UIKeyboardWillShowNotification以及UIKeyboardWillHideNotification来监测键盘的显示与隐藏事件。综合处理下,可以用以下方法解决:

  1. #ifndef IOS_VERSION  
  2. #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  
  3. #endif  


在viewdidload中注册监测事件:

  1. if (IOS_VERSION < 5.0) {  
  2.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillShowNotification object:nil];  
  3.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillHideNotification object:nil];  
  4. }else{  
  5.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];  
  6. }  


在dealloc中移除监测事件:

  1. if (IOS_VERSION < 5.0) {  
  2.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];  
  3.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];  
  4. }else{  
  5.     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];  
  6. }  


事件处理函数:

  1. - (void)keyboardWillChangeFrame:(NSNotification *)notification{  
  2.     if(!isDisplayFaceBox){  
  3. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2  
  4.         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {  
  5. #endif  
  6. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2  
  7.             NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];  
  8. #else  
  9.             NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];  
  10. #endif  
  11.             CGRect keyboardBounds;  
  12.             [keyboardBoundsValue getValue:&keyboardBounds];  
  13.             //UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);  
  14.             //[keyboardScrollView setScrollIndicatorInsets:e];  
  15.             //[keyboardScrollView setContentInset:e];  
  16.               
  17.             NSInteger offset = 480-keyboardBounds.origin.y;  
  18.             CGRect listFrame = CGRectMake(0, offset, 320, 377-offset);  
  19.               
  20.             [UIView beginAnimations:@"anim" context:NULL];  
  21.             [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
  22.             [UIView setAnimationBeginsFromCurrentState:YES];  
  23.             [UIView setAnimationDuration:0.3];  
  24.             //处理移动事件,将各视图设置最终要达到的状态  
  25.             [tableviewbo setFrame:listFrame];  
  26.               
  27.             faceButton.hidden = NO;  
  28.             keyboardButton.hidden = YES;      
  29.               
  30.             [keyboardScrollView setContentOffset:CGPointMake(0, offset) animated:NO];  
  31.               
  32.             [self scrollToBottomAnimated:NO];  
  33.               
  34.             [UIView commitAnimations];  
  35.               
  36. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2  
  37.         }  
  38. #endif  
  39.     }  
  40. }  

抱歉!评论已关闭.