今天我來講一下鍵盤遮擋輸入框時的解決方法。我做的一個界面是這樣的
我的輸入框是在最下面,如果不做相關的操作的話,當編輯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]; } }
這個是編輯輸入框時的效果
這個是鍵盤切換成中文輸入法時的效果