美文网首页
iOS检测键盘弹出与隐藏

iOS检测键盘弹出与隐藏

作者: 羊皮艾吉斯 | 来源:发表于2018-08-24 17:42 被阅读21次
[center addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
//  键盘弹出触发该方法
- (void)keyboardDidShow:(NSNotification *)aNotification {
    NSLog(@"键盘弹出");
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
}
//  键盘隐藏触发该方法
- (void)keyboardDidHide:(NSNotification *)aNotification {
    NSLog(@"键盘隐藏");
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

相关文章

网友评论

      本文标题:iOS检测键盘弹出与隐藏

      本文链接:https://www.haomeiwen.com/subject/avdiiftx.html