上一篇中我們把輸入框和按鈕歸為了一組虑啤,按回車可以跳到下一項(xiàng)辐啄。接下來我們要獲取鍵盤高度并對UIView進(jìn)行調(diào)整漓骚。
1.先補(bǔ)充一些成員變量
//.m
@interface ViewController ()<UITextFieldDelegate> {
BOOL _isKeyboardShowing;
UITextField *_activeTextField;
BOOL _addedObserver;
NSArray *_inputFocuses;
BOOL _pressedReturn;
//-----------新代碼-----------
float _keyboardHeight;
BOOL _invokeDelegateNow;
//---------------------------
}
2.接下來先從最簡單的方法實(shí)現(xiàn)起來。隱藏鍵盤的時(shí)候恢復(fù)視圖原有位置。
//.m
//-----------新代碼-----------
- (void)hideKeyBoard {
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//將視圖的Y坐標(biāo)向上移動(dòng)offset個(gè)單位耽梅,以使下面騰出地方用于軟鍵盤的顯示
self.view.bounds =CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
[UIView commitAnimations];
}
//---------------------------
3.實(shí)現(xiàn)KeyboardWillHide:消息。如果點(diǎn)擊回車切換到下一個(gè)輸入框胖烛,鍵盤會(huì)先消失眼姐,也會(huì)觸發(fā)這個(gè)消息诅迷。所以我們只需要在點(diǎn)擊輸入框之外的時(shí)候隱藏鍵盤。否則視圖會(huì)先回到原位再根據(jù)下一個(gè)文本框位置調(diào)整自己的位置众旗,視覺上不連貫罢杉。
//.m
- (void)KeyboardWillHide:(NSNotification *)notification {
//-----------新代碼-----------
_isKeyboardShowing = NO;
if (!_pressedReturn) {
[self hideKeyBoard];
}
else {
_pressedReturn = NO;
}
//---------------------------
}
- KeyboardWillShow:消息中獲得鍵盤高度以后手動(dòng)調(diào)用一次鍵盤代理方法textFieldDidBeginEditing:
//.m
- (void)KeyboardWillShow:(NSNotification *)notification {
_isKeyboardShowing = YES;
//-----------新代碼-----------
NSDictionary *info = [notification userInfo];
//獲取高度
NSValue *value = [info objectForKey:@"UIKeyboardBoundsUserInfoKey"];
if (!value) {
@throw [NSException exceptionWithName:@"error when getting Keyboard Rect" reason:@"maybe not supported in sdk" userInfo:nil];
}
CGSize keyboardSize = [value CGRectValue].size;
_keyboardHeight = keyboardSize.height;
_invokeDelegateNow = YES;
[self textFieldDidBeginEditing:_activeTextField];
//---------------------------
}
5.回到鍵盤代理方法之前先實(shí)現(xiàn)一個(gè)根據(jù)輸入框位置調(diào)整UIView的方法。
//.m
//-----------新代碼-----------
- (void)autoAdjustTextFieldHeight:(UITextField *)textField
{
CGRect frame = textField.frame;
CGPoint point = frame.origin;
CGRect viewBounds = self.view.bounds;
int offset = point.y + frame.size.height - (viewBounds.size.height - _keyboardHeight);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//將視圖的Y坐標(biāo)向上移動(dòng)offset個(gè)單位贡歧,以使下面騰出地方用于軟鍵盤的顯示
self.view.bounds = CGRectMake(0, offset > 0 ? ABS(offset) : 0, viewBounds.size.width, viewBounds.size.height);
[UIView commitAnimations];
}
//---------------------------
6.回到textFieldDidBeginEditing:滩租,根據(jù)_invokeDelegateNow來決定是否調(diào)整視圖高度。
//.m
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_pressedReturn = NO;//按下return和直接點(diǎn)擊text field都會(huì)觸發(fā)這個(gè)方法利朵,所以用一個(gè)標(biāo)示的變量來區(qū)分律想,后面會(huì)發(fā)揮作用的。
_activeTextField = textField;
//-----------新代碼-----------
//重要:這里的_invokeDelegateNow標(biāo)記的作用是這樣的绍弟,用戶點(diǎn)擊文本框會(huì)先觸發(fā)這個(gè)代理方法技即,此時(shí)沒有從KeyboardWillShow:的實(shí)現(xiàn)中獲取鍵盤高度。_invokeDelegateNow會(huì)在KeyboardWillShow:中被標(biāo)記為YES然后手動(dòng)再次調(diào)用這個(gè)代理方法(不是由系統(tǒng)調(diào)用)樟遣。此時(shí)再執(zhí)行調(diào)整視圖高度的操作而叼,才能根據(jù)鍵盤高度來調(diào)整。
if (_invokeDelegateNow) {
[self autoAdjustTextFieldHeight:textField];
}
//---------------------------
}
7.最后更新一下回車的方法豹悬。當(dāng)回車以后的操作為點(diǎn)擊按鈕時(shí)葵陵,隱藏鍵盤。當(dāng)回車的操作為跳到下一個(gè)輸入框時(shí)瞻佛,調(diào)整視圖位置脱篙。
//.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
int index = [_inputFocuses indexOfObject:textField];
if (index == [_inputFocuses count] - 1) {//如果是最后一個(gè)text field時(shí)按return 就點(diǎn)擊按鈕
[self click:_button];
[self hideKeyBoard];
}
else {//如果沒有到最后一個(gè)text field就將焦點(diǎn)轉(zhuǎn)向下一個(gè)text field
UITextField *nextTextField = [_inputFocuses objectAtIndex:(index + 1)];
[nextTextField becomeFirstResponder];
[self autoAdjustTextFieldHeight:nextTextField];
}
UITextField *currentTextField = [_inputFocuses objectAtIndex:index];
[currentTextField resignFirstResponder];
return NO;
}
這就是我的解決方案,希望能夠幫助到大家涤久。如果代碼用下來有什么問題,請留言忍弛。
工程已上傳GitHub:https://github.com/manuqiao/TextFieldAutoAdjustment