如何解决由引起的IQKeyboardManager部分页面返回的键盘高度比实际小或最后收到键盘隐藏通知却显示了键盘问题

简介: 如何解决由引起的IQKeyboardManager部分页面返回的键盘高度比实际小或最后收到键盘隐藏通知却显示了键盘问题

部分页面需要显示一个工具栏,当键盘出现时,这个工具栏显示在键盘上方。由于使用了IQKeyboardManager,导致有的页面iOS12.5.6系统的手机出现首次键盘高度比实际低44像素,而iOS14.7.1系统的手机出现首次键盘高度比实际低44像素并且有时出现最后收到键盘隐藏通知但是实际显示了键盘。

收到通知直接设置工具栏的frame,也过于突兀,最好采用做动画的方式显示工具栏,动画完成时再修正键盘高度,这样能部分解决上面的那两个问题。

那键盘的动画时间是0.25秒,可以在键盘通知里获取到这个参数。那键盘的实际高度从哪里获取呢?经过观察View UI Herarchy发现有键盘的页面都有UIInputSetContainerView,键盘的总高度就是UIInputSetHostView的总高度。只是iOS13及跟高的系统,键盘是在第二个窗口的UIInputWindowController中,而比iOS13低的版本在第是哪个窗口的UITextEffectsWindow中。具体的实现如下:

增加键盘显示通知:

        //监听键盘的变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardChange:) name:UIKeyboardWillShowNotification object:nil];

键盘通知处理:

- (void)keyBoardChange:(NSNotification *)notification {
    //获取键盘的高度
        NSDictionary *userInfo = [notification userInfo];
        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [aValue CGRectValue];
        CGFloat height = keyboardRect.size.height;
    //获取动画时间I
    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSLog(@"animationDuration:%f,height:%f",animationDuration,height);
    

    if(self.keyBoardChangeBlock)
    {
        self.keyBoardChangeBlock(height, animationDuration);
    }
}

键盘显示动画与修正:

- (LCCommentToolView *)comTool {
    if (_comTool == nil) {
        _comTool  = [[LCCommentToolView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, BaseSize(100))] ;
        [_comTool setCorner:UIRectCornerTopLeft|UIRectCornerTopRight cornerSize:BaseSize(15) forRect:CGRectMake(0, 0, KScreenW, BaseSize(100))];
        DYWeakSelf;
        [_comTool setKeyBoardChangeBlock:^(CGFloat height, CGFloat animationDuration) {
            if (DYGlobleData.isLogin) {
                if(animationDuration <= 0)
                {
                    //防范式编程,理论不会出现
                    weakSelf.bgV.hidden = NO;
                }
                else
                {
                    weakSelf.bgV.hidden = (height<=0);
                    [UIView animateWithDuration:animationDuration animations:^{
                        if((height<=0))
                        {
                            [weakSelf.comTool mas_updateConstraints:^(MASConstraintMaker *make) {
                                make.height.mas_equalTo(BaseSize(100));
                                make.width.mas_equalTo(KScreenW);
                                make.bottom.mas_equalTo(BaseSize(100) -(49+(BR_BOTTOM_MARGIN?24:0)));
                            }];
                            [weakSelf.comTool updateCommentTVWithShift:(BaseSize(100) -(49+(BR_BOTTOM_MARGIN?24:0))+(BR_BOTTOM_MARGIN?24:0))];
                        }
                        else
                        {
                            [weakSelf.comTool mas_updateConstraints:^(MASConstraintMaker *make) {
                                make.height.mas_equalTo(BaseSize(100));
                                make.width.mas_equalTo(KScreenW);
                                make.bottom.mas_equalTo(-height);
                            }];
                            [weakSelf.comTool updateCommentTVWithShift:0];
                        }
                        [weakSelf layoutIfNeeded];
                    } completion:^(BOOL finished) {
                        CGFloat keyboardHeight = [weakSelf displayKeyboardDockView];
                        if(keyboardHeight > 0)
                        {
                            [weakSelf.comTool mas_updateConstraints:^(MASConstraintMaker *make) {
                                make.height.mas_equalTo(BaseSize(100));
                                make.width.mas_equalTo(KScreenW);
                                make.bottom.mas_equalTo(-keyboardHeight);
                            }];
                            [weakSelf.comTool updateCommentTVWithShift:0];
                        }
                        else
                        {
                            [weakSelf.comTool mas_updateConstraints:^(MASConstraintMaker *make) {
                                make.height.mas_equalTo(BaseSize(100));
                                make.width.mas_equalTo(KScreenW);
                                make.bottom.mas_equalTo(BaseSize(100) -(49+(BR_BOTTOM_MARGIN?24:0)));
                            }];
                            [weakSelf.comTool updateCommentTVWithShift:(BaseSize(100) -(49+(BR_BOTTOM_MARGIN?24:0))+(BR_BOTTOM_MARGIN?24:0))];
                        }
                        [weakSelf layoutIfNeeded];
                    }];
                }
            } else {
                weakSelf.bgV.hidden = YES;
                [weakSelf.comTool mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(BaseSize(100));
                    make.width.mas_equalTo(KScreenW);
                    make.bottom.mas_equalTo(BaseSize(100) -(49+(BR_BOTTOM_MARGIN?24:0)));
                }];
                [weakSelf.comTool updateCommentTVWithShift:(BaseSize(100) -(49+(BR_BOTTOM_MARGIN?24:0))+(BR_BOTTOM_MARGIN?24:0))];
            };
        }];
    }
    return _comTool;
}

识别键盘是否在显示和获取键盘高度,键盘高为0.0是键盘隐藏:

- (CGFloat)displayKeyboardDockView{
    UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtSafeIndex:1];
    UIView *keyboardContainerView;
    UIView *keyboardHostView;
 
    if (@available(iOS 13.0, *))
    {
        for(int i = 0; i < [keyboardWindow.subviews count]; i++) {
            keyboardContainerView = [keyboardWindow.subviews objectAtIndex:i];
            UIViewController *nextResponder = (UIViewController*)[keyboardContainerView nextResponder];
            if(([nextResponder isKindOfClass:NSClassFromString(@"UIWindow")]))
            {
                NSLog(@"[nextResponder.subviews count]:%d, nextResponder.subviews:%@", [keyboardWindow.subviews count], keyboardWindow.subviews);
            }
            //寻找keyboardWindow层的UIInputWindowController
            else if ([nextResponder isKindOfClass:NSClassFromString(@"UIInputWindowController")]) {
                for(int y = 0; y < [nextResponder.view.subviews count]; y++) {
                    //寻找UIInputWindowController层的UIInputSetContainerView
                    if([[keyboardContainerView description] hasPrefix:@"<UIInputSetContainerView"] == YES){
                        for(int t = 0; t < [keyboardContainerView.subviews count]; t++) {
                            keyboardHostView = [keyboardContainerView.subviews objectAtIndex:t];
                            //寻找UIInputSetContainerView层的UIInputSetHostView
                            if([[keyboardHostView description] hasPrefix:@"<UIInputSetHostView"] == YES){
                                NSLog(@"keyboardHostView description:%@,keyboardHostView.isHidden:%d,[keyboardHostView.subviews count],[keyboardHostView.subviews count],keyboardHostView.frame.size.height:%f,keyboardHostView.frame.origin.y:%f, (keyboardHostView.frame.origin.y == [[UIScreen mainScreen] bounds].size.height):%d", [keyboardHostView description],keyboardHostView.isHidden,[keyboardHostView.subviews count], keyboardHostView.frame.size.height,keyboardHostView.frame.origin.y, (keyboardHostView.frame.origin.y == [[UIScreen mainScreen] bounds].size.height));
                                if (lroundl(keyboardHostView.frame.origin.y) != lroundl(([[UIScreen mainScreen] bounds].size.height)))
                                {
                                    return keyboardHostView.frame.size.height;
                                }
                                else
                                {
                                    return 0.0;
                                }
                            }
                        }
                    }
                }
            }
        }
        return 0.0;
    }
    else
    {
        keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtSafeIndex:2];
        if(keyboardWindow && [keyboardWindow isKindOfClass:NSClassFromString(@"UITextEffectsWindow")] && !isCommonUnitEmptyArray(keyboardWindow.subviews))
        {
            keyboardContainerView = [keyboardWindow.subviews objectAtSafeIndex:0];
            if(keyboardContainerView && ([[keyboardContainerView description] hasPrefix:@"<UIInputSetContainerView"] == YES))
            {
                for(int t = 0; t < [keyboardContainerView.subviews count]; t++) {
                    keyboardHostView = [keyboardContainerView.subviews objectAtIndex:t];
                    //寻找UIInputSetContainerView层的UIInputSetHostView
                    if([[keyboardHostView description] hasPrefix:@"<UIInputSetHostView"] == YES){
                        NSLog(@"keyboardHostView description:%@,keyboardHostView.isHidden:%d,[keyboardHostView.subviews count],[keyboardHostView.subviews count],keyboardHostView.frame.size.height:%f,keyboardHostView.frame.origin.y:%f, (keyboardHostView.frame.origin.y == [[UIScreen mainScreen] bounds].size.height):%d", [keyboardHostView description],keyboardHostView.isHidden,[keyboardHostView.subviews count], keyboardHostView.frame.size.height,keyboardHostView.frame.origin.y, (keyboardHostView.frame.origin.y == [[UIScreen mainScreen] bounds].size.height));
                        if (lroundl(keyboardHostView.frame.origin.y) != lroundl(([[UIScreen mainScreen] bounds].size.height)))
                        {
                            return keyboardHostView.frame.size.height;
                        }
                        else
                        {
                            return 0.0;
                        }
                    }
                }
            }
        }
        return 0.0;
    }
   
}
目录
相关文章
|
人工智能 小程序 前端开发
【uniapp小程序】uploadFile文件上传
【uniapp小程序】uploadFile文件上传
2362 0
|
人工智能 测试技术 API
Apifox对比Apipost:2025年推荐的API协作工具
Apifox与Apipost这两大国产API平台的全方位较量,助你在2025年做出最明智的选择。
|
SQL 数据挖掘 关系型数据库
阿里云百炼|析言GBI全新发布:联合云上数据库,助力企业轻松实现ChatBI
析言GBI是阿里云推出的一款基于AI的智能数据分析产品,通过自然语言处理实现对话式数据分析。用户无需编写代码,即可轻松进行数据查询、分析和可视化。该产品支持多种数据库连接方式(如MySQL、PostgreSQL等),并提供多版本选择以适应不同业务需求。即将发布的动态规划BI分析功能将进一步提升复杂问题的拆解与综合分析能力。欢迎访问阿里云百炼应用广场体验析言GBI,并享受200次免费问题额度。
|
前端开发 开发者 UED
【Flutter前端技术开发专栏】Flutter中的手势识别与触摸事件处理
【4月更文挑战第30天】本文探讨了Flutter中的手势识别和触摸事件处理,关键点包括: 1. 使用`GestureRecognizer`类体系实现手势识别,如`TapGestureRecognizer`检测点击,`HorizontalDragGestureRecognizer`和`VerticalDragGestureRecognizer`识别滑动,`ScaleGestureRecognizer`识别捏合和扩张。
714 0
【Flutter前端技术开发专栏】Flutter中的手势识别与触摸事件处理
|
Web App开发
Chrome的插件扩展程序安装目录是什么?在哪个文件夹?
正常情况下,Chrome插件扩展程序的默认安装目录如下: 1.windows xp中chrome插件默认安装目录位置: C:\Documents and Settings\用户名\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions 2.
53372 2
|
安全 网络安全 Android开发
深度解析:利用Universal Links与Android App Links实现无缝网页至应用跳转的安全考量
【10月更文挑战第2天】在移动互联网时代,用户经常需要从网页无缝跳转到移动应用中。这种跳转不仅需要提供流畅的用户体验,还要确保安全性。本文将深入探讨如何利用Universal Links(仅限于iOS)和Android App Links技术实现这一目标,并分析其安全性。
2992 0
|
SQL 小程序 JavaScript
微信小程序登录(保持登录状态)
微信小程序登录(保持登录状态)
1117 1
|
SQL 存储 监控
Hive 插入大量数据
【8月更文挑战第15天】
1254 0
|
存储 安全 数据安全/隐私保护
kali工具 -- setoolkit(克隆网站及利用)
kali工具 -- setoolkit(克隆网站及利用)
1644 1