iOS5中当键盘输入法切换到中文时,键盘高度由216增加到252像素。这一变化将遮住输入框。如何才能解决这一问题呢?
在iOS5中,新增有notification(UIKeyboardWillChangeFrameNotification)可以用来监测键盘frame的变化。在iOS4中,可以通过UIKeyboardWillShowNotification以及UIKeyboardWillHideNotification来监测键盘的显示与隐藏事件。综合处理下,可以用以下方法解决:
- #ifndefIOS_VERSION
- #defineIOS_VERSION[[[UIDevicecurrentDevice]systemVersion]floatValue]
- #endif
在viewdidload中注册监测事件:
- if(IOS_VERSION<5.0){
- [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillShowNotificationobject:nil];
- [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillHideNotificationobject:nil];
- }else{
- [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillChangeFrameNotificationobject:nil];
- }
在dealloc中移除监测事件:
- if(IOS_VERSION<5.0){
- [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
- [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
- }else{
- [[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillChangeFrameNotificationobject:nil];
- }
事件处理函数:
- -(void)keyboardWillChangeFrame:(NSNotification*)notification{
- if(!isDisplayFaceBox){
- #if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_3_2
- if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone){
- #endif
- #if__IPHONE_OS_VERSION_MIN_REQUIRED>=__IPHONE_3_2
- NSValue*keyboardBoundsValue=[[notificationuserInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
- #else
- NSValue*keyboardBoundsValue=[[notificationuserInfo]objectForKey:UIKeyboardBoundsUserInfoKey];
- #endif
- CGRectkeyboardBounds;
- [keyboardBoundsValuegetValue:&keyboardBounds];
- //UIEdgeInsetse=UIEdgeInsetsMake(0,0,keyboardBounds.size.height,0);
- //[keyboardScrollViewsetScrollIndicatorInsets:e];
- //[keyboardScrollViewsetContentInset:e];
- NSIntegeroffset=480-keyboardBounds.origin.y;
- CGRectlistFrame=CGRectMake(0,offset,320,377-offset);
- [UIViewbeginAnimations:@"anim"context:NULL];
- [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIViewsetAnimationBeginsFromCurrentState:YES];
- [UIViewsetAnimationDuration:0.3];
- //处理移动事件,将各视图设置最终要达到的状态
- [tableviewbosetFrame:listFrame];
- faceButton.hidden=NO;
- keyboardButton.hidden=YES;
- [keyboardScrollViewsetContentOffset:CGPointMake(0,offset)animated:NO];
- [selfscrollToBottomAnimated:NO];
- [UIViewcommitAnimations];
- #if__IPHONE_OS_VERSION_MAX_ALLOWED>=__IPHONE_3_2
- }
- #endif
- }
- }