iPhone 設備上,由於鍵盤是從下彈出,每每會致使文字輸入框被彈出的鍵盤遮住狀況。 此類問題,一種解決辦法是: 將父視圖往上移動必定的距離,讓文件輸入框顯示出來,不被鍵盤遮住。ide
好比:ui
有一個UITextField* 子視圖位於view視圖上,靠近屏幕下部,當鍵盤彈出時,將會被鍵盤遮住;spa
該例子的具體解決作法是:
code
一、關注鍵盤彈出、收回的兩個系統通知:server
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
二、在完成文字輸入後,想辦法讓鍵盤收回; 好比,在點擊視圖空白區域時,讓鍵盤收回;get
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tap.numberOfTapsRequired = 1; tap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tap]; // 點擊空白區,讓鍵盤收回 (void)tap:(UIGestureRecognizer *)gesture { [[self firstResponderFromeParentView:self.view] resignFirstResponder]; }
三、在處理鍵盤彈出通知的方法裏,經過計算將父視圖往上移動算出的距離;animation
- (void)keyboardWillShow:(NSNotification *)notification { // 當前視圖 UIView* parentView = self.view; UIView* subView = [self firstResponderFromeParentView:parentView]; // 鍵盤屬性 NSDictionary* userInfo = [notification userInfo]; // Get the origin of the keyboard when it's displayed. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. CGRect keyboardRect = [aValue CGRectValue]; // 在屏幕座標系 CGFloat keyboradHeight = keyboardRect.size.height; CGRect fieldFrame = [parentView convertRect:subView.frame toView:nil]; // 將子視圖在父視圖裏的座標,轉換成window裏的座標 CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat subViewBottom = screenRect.size.height - CGRectGetMaxY(fieldFrame); // 子視圖下部距屏幕底部距離 if (subViewBottom < keyboradHeight) { // 鍵盤蓋住了子視圖,須要上移父視圖 CGFloat moveupOffset = keyboradHeight - subViewBottom + fieldFrame.size.height; CGRect frame = parentView.frame; frame.origin.y = -moveupOffset; // Get the duration of the animation. NSValue* animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:animationDuration]; //self.tableview.frame = frame; parentView.frame = frame; [UIView commitAnimations]; } }
四、在處理鍵盤收回的通知方法裏,將父視圖還原到原來的位置;it
- (void)keyboardWillHide:(NSNotification *)notification { // 當前視圖 UIView* parentView = self.view; // NSDictionary* userInfo = [notification userInfo]; // Get the origin of the keyboard when it's displayed. //NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. //CGRect keyboardRect = [aValue CGRectValue]; CGRect frame = parentView.frame; frame.origin.y = 0; //keyboardRect.size.height; // Get the duration of the animation. NSValue* animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:animationDuration]; parentView.frame = frame; [UIView commitAnimations]; }
輔助方法:io
- (UIView *)firstResponderFromeParentView:(UIView *)parentView { for (UIView* view in parentView.subviews) { if ([view isFirstResponder]) { return view; } } return nil; }