欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS 返回键盘的实时高度(含三方键盘)

程序员文章站 2022-05-28 22:58:00
...
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
            name:UIKeyboardWillShowNotification
          object:nil];

#pragma mark -- 键盘显示 --
- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    double duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGFloat curkeyBoardHeight = [[info objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
    CGRect begin = [[info objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
    CGRect end = [[info objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];

    CGFloat keyBoardHeight;

    /*! 第三方键盘回调三次问题,监听仅执行最后一次 */
    if(begin.size.height > 0 && (begin.origin.y - end.origin.y > 0)){
        keyBoardHeight = curkeyBoardHeight;
        [UIView animateWithDuration:duration animations:^{
            NSLog(@"键盘高度%f",keyBoardHeight);
        }];
    }
}