键盘遮挡UITextField

”##“标记的是解决问题的代码

问题的关键是计算键盘的高度及变化,以及文本框的位置,文本框位置随着键盘变化。
但是UIKeyboardFrameBeginUserInfoKey通知会通知键盘调整前高度和调整后高度,但是并不准确,所以算法会有一定问题。

计算文本框位置:
1、文本框.y = 文本框父视图.height - 此时键盘高度.height - 文本框自身.height
2、文本框.y = 此时键盘.y + 文本框自身.height

- (void)duration:(CGFloat)duration EndF:(CGRect)endF {
  [UIView animateWithDuration:duration animations:^{
   self.frame = endF;
   ##_resignButton.frame = CGRectMake(_resignButton.frame.origin.x, _resignButton.frame.origin.y, _resignButton.frame.size.width, SCREEN_HEIGHT - (self.superview.frame.size.height - endF.origin.y));
}];

}

pragma mark - 系统键盘通知事件

//  键盘即将显示     
- (void)keyBoardShow:(NSNotification* )noti {
if (!_textView.isFirstResponder) {
    return;
}
_resignButton.hidden = NO;
[self textViewChangeText];
CGRect begin = [[noti.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endF = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect fram = self.frame;
## fram.origin.y = self.superview.frame.size.height - endF.size.height - fram.size.height;
[self duration:duration EndF:fram];}


//  键盘即将隐藏
- (void)keyBoardHiden:(NSNotification *)noti {
if (_resignButton.hidden == YES) {
    return;
}

CGRect endF = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect fram = self.frame;
fram.size.height = _textViewOneHeight + _textViewOneSpaceHeight + TextViewTop * 2;
##fram.origin.y = self.superview.frame.size.height - fram.size.height;
[self duration:duration EndF:fram];

fram = _textView.frame;
fram.size.height = _textViewOneHeight + _textViewOneSpaceHeight;
[_textView setContentOffset:CGPointMake(0, _textView.contentSize.height - fram.size.height - _textViewOneSpaceHeight / 2.0) animated:NO];
_textView.frame = fram;

}

你可能感兴趣的:(键盘遮挡UITextField)