UIKeyboardTypeDecimalPad 價(jià)格鍵盤
- 設(shè)置tableView滾動(dòng)時(shí)鍵盤收起
tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
獲取系統(tǒng)鍵盤的相關(guān)信息
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
// 鍵盤的高度
CGFloat keyBordH = keyboardRect.size.height;
//鍵盤彈出需要的時(shí)間
double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
- 監(jiān)聽鍵盤彈窗液样、隱藏的通知一個(gè)方法就可以搞定
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)keyboardWillChangeFrame:(NSNotification *)note {
// 取出鍵盤最終的frame
CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//判斷鍵盤升起還是落下
CGFloat H = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
// 取出鍵盤彈出需要花費(fèi)的時(shí)間
double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
if (H == 0) { //鍵盤落下
make.bottom.equalTo(self.view).offset(- 60 );
} else { //升起
make.bottom.equalTo(self.view).offset( - H);
}
}];
//過渡動(dòng)畫
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
任務(wù)評(píng)價(jià)輸入框處理方案柿顶,之前開發(fā)項(xiàng)目鍵盤的處理一般都是用
IQKeyboardManager
第三方庫處理的棍矛,方便快捷硬贯。但是順豐的項(xiàng)目之前沒有用這個(gè)庫,所以不可能像之前小團(tuán)隊(duì)開發(fā)那樣隨便拿來就用了龙致,只能自己處理鍵盤的遮擋和隱藏了
要處理的界面1.png
要處理的界面2.png
第一種方法:計(jì)算處理(比較容易出錯(cuò)绩脆,推薦使用第二種方法)
- (void)keyboardWillShow:(NSNotification *)notification {
//獲取系統(tǒng)鍵盤的相關(guān)信息
NSDictionary *userInfo = [notification userInfo];
NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [value CGRectValue];
//獲取鍵盤相對(duì)于self.view的frame ,傳window和傳nil是一樣的
keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];
CGFloat keyboardTop = keyboardRect.origin.y;
CGFloat keyBordH = keyboardRect.size.height;
//輸入框所在的cell相對(duì)于self.view的frame
CGRect rect = [self.inputCell.superview convertRect:self.inputCell.frame toView:self.view];
CGFloat inputCellBottom = CGRectGetMaxY(rect);
//輸入cell被鍵盤遮擋
if (keyboardTop < inputCellBottom) {
//計(jì)算view偏移量
CGFloat gap = keyboardTop - inputCellBottom;
if (fabs(gap) > keyBordH) {
//view的偏移量大于鍵盤高度
self.view.transform = CGAffineTransformMakeTranslation(0, -keyBordH);
[self.tableView setContentOffset:CGPointMake(0, (self.tableView.contentOffset.y + (fabs(gap) - keyBordH))) animated:NO];
} else {
//view的偏移量小于鍵盤高度
self.view.transform = CGAffineTransformMakeTranslation(0, gap);
}
}
}
//當(dāng)鍵退出
- (void)keyboardWillHide:(NSNotification *)notification {
//鍵盤退出 還原view的位置到原始位置
self.view.transform = CGAffineTransformIdentity;
}
難點(diǎn)1 就是計(jì)算偏移量
思路就是把輸入框所在cell的frame和鍵盤view的frame都進(jìn)行坐標(biāo)轉(zhuǎn)換暇藏,轉(zhuǎn)換成針對(duì)self.view的坐標(biāo)
難點(diǎn)2 就是根據(jù)計(jì)算的偏移量改變view的frame
- 先判斷輸入框所在的cell是否被鍵盤遮擋蜜笤,如果沒有就不處理
- 如果遮擋了,但是偏移量
小于
鍵盤高度直接偏移self.view的frame即可 - 如果遮擋了盐碱,但是偏移量
大于
鍵盤高度把兔,設(shè)置view的偏移量為鍵盤高度,同事設(shè)置tableview的contentOffset值即可
第二種簡(jiǎn)單的處理方法瓮顽,推薦使用這種
晚上過代碼時(shí)县好,登哥說你這樣太麻煩了而且容易出錯(cuò),有更簡(jiǎn)單的處理方法暖混,就是利用系統(tǒng)的UITableViewController來進(jìn)行處理聘惦。
- (UITableView *)tableView {
if (!_tableView) {
UITableViewController *controller = [[UITableViewController alloc] init];
controller.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView = controller.tableView;
_tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
[self addChildViewController:controller];
[controller didMoveToParentViewController:self];
}
return _tableView;
}
因?yàn)轫?xiàng)目中的控制器都是繼承的公共的基類控制器,所在這里不能繼承UITableViewController儒恋,就用了上面的取巧的方法善绎,然后試了一下果然奏效,登哥很給力诫尽!