唤醒键盘后无法立即隐藏键盘问题与隐藏键盘的四种方式
创始人
2024-03-02 12:11:58
0

客户有一个需求,唤醒键盘时判断是否登录,若没有登录就进入登录并且收起键盘。
我开始的做法是收到键盘显示通知后隐藏键盘,但是无论调用那种方式的隐藏键盘,键盘都收不起来,估计有可能和IQKeyboardManager有关。
在输入控件实现了tv.delegate = self;,先看隐藏键盘的方式:
1.注销第一响应者:resignFirstResponder
2.结束编辑(你的View必须是继承于UIControl):[self endEditing:YES];[self.view endEditing:YES];;
3.keyWindow结束编辑,可以用来统一收起键盘[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
4.直接发送 resignFirstResponder 消息:[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]
我的输入框是加在keyWindow上的。试过以上四种方法全部无效。

      [_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))];[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
//                [weakSelf layoutIfNeeded];[weakSelf.comTool resignFirstResponder];[weakSelf endEditing:YES];if(weakSelf.endEditingBlock){weakSelf.endEditingBlock();}LCAccountLogController *login = [[LCAccountLogController alloc] init];DY_NavViewController *nav = [[DY_NavViewController alloc] initWithRootViewController:login];nav.modalPresentationStyle = 0;[[FMConfig config].window.rootViewController presentViewController:nav animated:YES completion:nil];};}];

既然显示键盘后立即收起不了,那么换一个思路:当判断是否显示键盘时,当发现没有登录时直接返回不显示键盘,并进行后续处理。

输入框所在控件判断是否需要显示键盘:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{if(self.checkCannotBeginEditingBlock){self.checkCannotBeginEditingBlock();return !self.cannotBeginEditing;}else{return YES;}
}

输入框的父控件进行具体的键盘是否显示判断:

        _comTool.checkCannotBeginEditingBlock = ^{if (DYGlobleData.isLogin){weakSelf.comTool.cannotBeginEditing = NO;}else{if(DYGlobleData.isInLogin){return;}weakSelf.bgV.hidden = YES;weakSelf.comTool.cannotBeginEditing = YES;LCAccountLogController *login = [[LCAccountLogController alloc] init];[DYGetCurrentViewController().navigationController pushViewController:login animated:YES];}};

相关内容

热门资讯

美国2年期国债收益率上涨15个... 原标题:美国2年期国债收益率上涨15个基点 美国2年期国债收益率上涨15个基...
汽车油箱结构是什么(汽车油箱结... 本篇文章极速百科给大家谈谈汽车油箱结构是什么,以及汽车油箱结构原理图解对应的知识点,希望对各位有所帮...
嵌入式 ADC使用手册完整版 ... 嵌入式 ADC使用手册完整版 (188977万字)💜&#...
重大消息战皇大厅开挂是真的吗... 您好:战皇大厅这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...
盘点十款牵手跑胡子为什么一直... 您好:牵手跑胡子这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游...
senator香烟多少一盒(s... 今天给各位分享senator香烟多少一盒的知识,其中也会对sevebstars香烟进行解释,如果能碰...
终于懂了新荣耀斗牛真的有挂吗... 您好:新荣耀斗牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信8435338】很多玩家在这款游戏...
盘点十款明星麻将到底有没有挂... 您好:明星麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【5848499】很多玩家在这款游戏...
总结文章“新道游棋牌有透视挂吗... 您好:新道游棋牌这款游戏可以开挂,确实是有挂的,需要了解加客服微信【7682267】很多玩家在这款游...
终于懂了手机麻将到底有没有挂... 您好:手机麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8435338】很多玩家在这款游戏...