如上图所示,需要编辑用户的信息,键盘需要根据所输入的行来合理的显示键盘位置
①,文本框添加监听
//添加监听
[self addRegisterText:leftTitleRightText.textFieldRight];
//添加监听
- (void)addRegisterText:(DoneTextField *)textField
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEdit:) name:UITextFieldTextDidBeginEditingNotification object:textField];
}
②,键盘添加监听
//监听键盘,添加监听
[self addNotification];
- (void)addNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//键盘弹出
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *keyInfo = [notification userInfo];
//获取高度
id keyBoardValue = [keyInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyBoardSize = [keyBoardValue CGRectValue].size;
//获取键盘弹出时间
id keyBoardDurationValue = [keyInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
//如果键盘盖住文本框,则移动
CGFloat allH = CGRectGetMaxY(self.currentOperateTextRect)+keyBoardSize.height;
if (allH > (kScreenHeight-kNavH)) {
CGPoint backScrollViewPoint = self.backScrollView.contentOffset;
//相差的距离
backScrollViewPoint.y = allH - kScreenHeight;
//动画效果
[UIView animateWithDuration:[keyBoardDurationValue floatValue] animations:^{
self.backScrollView.contentOffset = backScrollViewPoint;
} completion:^(BOOL finished) {
}];
}
}
//键盘隐藏
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *keyInfo = [notification userInfo];
//获取高度
id keyBoardValue = [keyInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyBoardSize = [keyBoardValue CGRectValue].size;
//获取键盘弹出时间
id keyBoardDurationValue = [keyInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
CGPoint backScrollViewPoint = self.backScrollView.contentOffset;
backScrollViewPoint.y = -kNavH;
//动画效果
[UIView animateWithDuration:[keyBoardDurationValue floatValue] animations:^{
self.backScrollView.contentOffset = backScrollViewPoint;
} completion:^(BOOL finished) {
}];
}
注意:@property (nonatomic,assign) CGRect currentOperateTextRect;
全局变量,记录激活那个文本框的frame