监听键盘高度变化,以及键盘自定义

本文探讨了如何通过自定义视图实现键盘布局的动态调整,重点介绍了使用Masonry库进行布局变化的监听和响应,包括键盘弹出时表情栏跟随键盘顶部的布局调整策略。同时,还涉及了自定义键盘视图的实现方法,为开发者提供了实用的技术指导。

如果做表情栏,输入框,一般都需要这个栏随着键盘的弹出而贴在键盘顶部

#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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值