ios 键盘

1、通过第三方框架管理所有的键盘事件

IQKeyboardManager

2、通过监听键盘动作来获取键盘

<b>通过NSNotificationCenter监听</b>
UIKeyboardDidShowNotification(键盘显示)
UIKeyboardWillHideNotification(键盘消失)
监听到这个动作的时候,调用的方法会有一个NSNotification的参数

// 监听键盘将要显示的动作
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    // 监听键盘将要消失的动作
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// 监听键盘显示
-(void)keyboardWillShow:(NSNotification*)noti{
    //获取用户信息
    NSDictionary *info = noti.userInfo;
    
    //获取键盘开始的frame
    NSValue *heightValue = info[UIKeyboardFrameBeginUserInfoKey];
    
    //将heightValue转化成cgRectValue   在获取size  在获取高度
    CGFloat height = [heightValue CGRectValue].size.height;
    
    //改变发送view的高度约束
    _viewBottom.constant = height;
    
    //定义存放键盘动画时间用来让发送的view进行动画效果
    NSTimeInterval time = 0;
    
    //获取键盘动画时长
    NSNumber *timeDur = info[UIKeyboardAnimationDurationUserInfoKey];
    
    //把值放给time
    [timeDur getValue:&time];
    
    //因为view会改变  所以layoutIfNeeded  刷新布局
    [UIView animateWithDuration:time animations:^{
        [self.view layoutIfNeeded];
    }];
    
}

swift的键盘://www.greatytc.com/p/4e755fe09df7

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容