如果做表情栏,输入框,一般都需要这个栏随着键盘的弹出而贴在键盘顶部
#pragma mark - 键盘代理
- (void)regNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)unregNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
//监听高度的变化,我这里主要使用Masonry来实现,换frame的话,则需要
//CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
//通过对比开始和结束的键盘Frame来控制表情栏的frame.orgin.y
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
double yOffset = kScreenHeight - endKeyboardRect.origin.y;
[UIView animateWithDuration:duration animations:^{
[_barView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(_barView.superview).offset(-yOffset);
}];
[_barView.superview layoutIfNeeded];
}];
}
- (void)dealloc
{
[self unregNotification];
}
键盘自定义
我们可以通过自定义的View去替换系统的键盘视图,这样子可以达到自定义的目标
CustomView *customView = [[CustomView alloc] init];
textView.inputView = customView;
//中途切换另外的新视图的话,请调用刷新的方法
[textView reloadInputViews];