iOS检测键盘弹出与隐藏

2018-08-24  本文已影响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];
}
上一篇下一篇

猜你喜欢

热点阅读