鍵盤遮擋UITextField

”##“標(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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容