在延展中加入
/**输入工具条底部的约束*/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputToolBarBottomConstraint;
修改 viewDidLoad方法如下
- (void)viewDidLoad {
[super viewDidLoad];
//1.监听键盘弹出,把inputToolbar(输入工具条)往上移
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillShow:) name:UIKeyboardWillShowNotification object:nil];
//2.监听键盘退出,inputToolbar恢复原位
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
在类中加入方法
#pragma mark 键盘显示时会触发的方法
-(void)kbWillShow:(NSNotification *)noti{
//1.获取键盘高度
//1.1获取键盘结束时候的位置
CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat kbHeight = kbEndFrm.size.height;
//2.更改inputToolbar底部约束
self.inputToolBarBottomConstraint.constant = kbHeight;
//添加动画
[UIView animateWithDuration:0.25 animations:^{
[self.view layoutIfNeeded];
}];
}
#pragma mark 键盘退出时会触发的方法
-(void)kbWillHide:(NSNotification *)noti{
//inputToolbar恢复原位
self.inputToolBarBottomConstraint.constant =0;
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}