”##“標(biāo)記的是解決問題的代碼
問題的關(guān)鍵是計算鍵盤的高度及變化,以及文本框的位置,文本框位置隨著鍵盤變化。
但是UIKeyboardFrameBeginUserInfoKey通知會通知鍵盤調(diào)整前高度和調(diào)整后高度,但是并不準(zhǔn)確,所以算法會有一定問題。
計算文本框位置:
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 - 系統(tǒng)鍵盤通知事件
// 鍵盤即將顯示
- (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;
}