1.界面布局
2.演示效果
3.聊天控制器代码如下:
//
// ChatViewController.m
#import "ChatViewController.h"
@interface ChatViewController ()
/**
* inputToolbar(输入工具栏)底部的约束
*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolbarBottomConstraint;
@end
@implementation ChatViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1.监听键盘的弹出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 2.监听键盘的退出
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
/**
* 键盘退出会调用
*/
- (void)keyboardWillHide:(NSNotification *)notice
{
// inputToolbar回到原位
self.inputToolbarBottomConstraint.constant = 0;
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];
}
/**
* 键盘弹出会调用
*/
- (void)keyboardWillShow:(NSNotification *)notice
{
// 1.获取键盘高度
CGFloat heigth = [notice.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
// 2.更改inputToolbar底部约束
self.inputToolbarBottomConstraint.constant = heigth;
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];
}
/**
* 添加通知要移除,我们可以在这里方法里移除
*/
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end