原文出处:http://blog.csdn.net/yangxt/article/details/8300970
今天我来讲一下键盘遮挡输入框时的解决方法。我做的一个界面是这样的
我的输入框是在最下面,如果不做相关的操作的话,当编辑UITextField的时候,弹出键盘就会挡着输入框,那怎么解决这个问题呢,我们可以考虑让输入框随键盘一起向上移动,当关闭键盘时让输入框也一起向下移动回到原来的位置,这样就可以解决键盘挡着输入框的问题啦
下面看具体的代码实现:
在ios5.0之前呢键盘高度固定是216像素高,而ios5.0之后包括ios5.0键盘的高度再不是固定的,当由英文切换在中文输入时,键盘由原来的216变成了252
先说ios5.0之前的解决办法吧,我是通过UITextField的委托方法-(void)textFieldDidBeginEditing:(UITextField *)textField和另一个关掉键盘的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField来实现的,点击键盘上的Return会关掉键盘的
1 - (void)textFieldDidBeginEditing:(UITextField *)textField 2 { 3 CGRect frame = textField.frame; 4 int offset = frame.origin.y +422 - (self.view.frame.size.height - 240);//键盘高度216 5 NSLog(@"offset is %d",offset); 6 NSTimeInterval animationDuration = 0.30f; 7 [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; 8 [UIView setAnimationDuration:animationDuration]; 9 float width = self.view.frame.size.width; 10 float height = self.view.frame.size.height; 11 if(offset > 0) 12 { 13 CGRect rect = CGRectMake(0.0f, -offset,width,height); 14 self.view.frame = rect; 15 } 16 [UIView commitAnimations]; 17 }
1 - (BOOL)textFieldShouldReturn:(UITextField *)textField 2 { 3 NSTimeInterval animationDuration = 0.30f; 4 [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; 5 [UIView setAnimationDuration:animationDuration]; 6 CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); 7 self.view.frame = rect; 8 [UIView commitAnimations]; 9 [textField resignFirstResponder]; 10 return YES; 11 }
对了在添加-(void)textFieldDidBeginEditing:(UITextField *)textField方法前要声明并设置delegate
另一个方法是针对ios5.0之后键盘高度随输入法不同变化时的解决办法,同时也适用于ios5.0之前
在-(void)viewDidLoad方法里面添加通知
1 if(IOS_VERSION<5.0) 2 { 3 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 4 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillHideNotification object:nil]; 5 }else{ 6 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 7 }
至于如何获取IOS系统版本号通过如下可获取
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
添加一个方法-(void)keyboardWillShow:(NSNotification *)notification具体如下
-(void)keyboardWillShow:(NSNotification *)notification { #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 = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; #else NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey]; #endif CGRect keyboardBounds; [keyboardBoundsValue getValue:&keyboardBounds]; NSInteger offset =self.view.frame.size.height-keyboardBounds.origin.y+64.0; CGRect listFrame = CGRectMake(0, -offset, self.view.frame.size.width,self.view.frame.size.height); NSLog(@"offset is %d",offset); [UIView beginAnimations:@"anim" context:NULL]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3]; //处理移动事件,将各视图设置最终要达到的状态 self.view.frame=listFrame; [UIView commitAnimations]; } }
这个是编辑输入框时的效果
这个是键盘切换成中文输入法时的效果