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

本文介绍了一种在iOS5中通过监测UIKeyboardWillChangeFrameNotification通知来解决键盘输入法切换到中文时高度增加导致遮挡输入框的问题的方法。包括在viewDidLoad中注册事件监听、在dealloc中移除监听,以及在事件处理函数中进行相应的布局调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

  1. #ifndefIOS_VERSION
  2. #defineIOS_VERSION[[[UIDevicecurrentDevice]systemVersion]floatValue]
  3. #endif

在viewdidload中注册监测事件:

  1. if(IOS_VERSION<5.0){
  2. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillShowNotificationobject:nil];
  3. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillHideNotificationobject:nil];
  4. }else{
  5. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillChangeFrameNotificationobject:nil];
  6. }

在dealloc中移除监测事件:

  1. if(IOS_VERSION<5.0){
  2. [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
  3. [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
  4. }else{
  5. [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillChangeFrameNotificationobject: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=[[notificationuserInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
  8. #else
  9. NSValue*keyboardBoundsValue=[[notificationuserInfo]objectForKey:UIKeyboardBoundsUserInfoKey];
  10. #endif
  11. CGRectkeyboardBounds;
  12. [keyboardBoundsValuegetValue:&keyboardBounds];
  13. //UIEdgeInsetse=UIEdgeInsetsMake(0,0,keyboardBounds.size.height,0);
  14. //[keyboardScrollViewsetScrollIndicatorInsets:e];
  15. //[keyboardScrollViewsetContentInset:e];
  16. NSIntegeroffset=480-keyboardBounds.origin.y;
  17. CGRectlistFrame=CGRectMake(0,offset,320,377-offset);
  18. [UIViewbeginAnimations:@"anim"context:NULL];
  19. [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
  20. [UIViewsetAnimationBeginsFromCurrentState:YES];
  21. [UIViewsetAnimationDuration:0.3];
  22. //处理移动事件,将各视图设置最终要达到的状态
  23. [tableviewbosetFrame:listFrame];
  24. faceButton.hidden=NO;
  25. keyboardButton.hidden=YES;
  26. [keyboardScrollViewsetContentOffset:CGPointMake(0,offset)animated:NO];
  27. [selfscrollToBottomAnimated:NO];
  28. [UIViewcommitAnimations];
  29. #if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_3_2
  30. }
  31. #endif
  32. }
  33. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值