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; }