如果做表情栏,输入框,一般都需要这个栏随着键盘的弹出而贴在键盘顶部
#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];
本文探讨了如何通过自定义视图实现键盘布局的动态调整,重点介绍了使用Masonry库进行布局变化的监听和响应,包括键盘弹出时表情栏跟随键盘顶部的布局调整策略。同时,还涉及了自定义键盘视图的实现方法,为开发者提供了实用的技术指导。

547

被折叠的 条评论
为什么被折叠?



