做即时通信界面时,有时候需要实现在输入框输入内容时,输入框随键盘弹起,而聊天的内容不会被盖在,也随之弹起
一:第一步其实很简单,监听键盘事件
//监听键盘
NSNotificationCenter * notification = [NSNotificationCenter defaultCenter];
//键盘位置变化
[notification addObserver:self selector:@selector(keyChangeFrom:) name:UIKeyboardWillChangeFrameNotification object:nil];
-(void)keyChangeFrom:(NSNotification *)notification{
//得到键盘y值
CGRect re = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat key = re.origin.y;
CGFloat offes = key - self.view.frame.size.height;
[UIView animateWithDuration:0.25 animations:^{
//整体向上偏移
self.view.transform = CGAffineTransformMakeTranslation(0, offes);
}];
//让UITableView最后一行显示到前面
NSIndexPath * index = [NSIndexPath indexPathForRow:self.array.count-1 inSection:0];
[self.table scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
RXSendInfor * inforBut = [[RXSendInfor alloc] init];
inforBut.frame = CGRectMake(0, CGRectGetMaxY(self.table.frame),self.view.frame.size.width, 46);
inforBut.textfield.delegate = self;//给文本框设置代理
[self.view addSubview:inforBut];
#pragma mark -----滚动时收回键盘
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//将键盘叫回去
[self.view endEditing:YES];
}