客户有一个需求,唤醒键盘时判断是否登录,若没有登录就进入登录并且收起键盘。
我开始的做法是收到键盘显示通知后隐藏键盘,但是无论调用那种方式的隐藏键盘,键盘都收不起来,估计有可能和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]; } };