公司项目需求,需要让弹窗显示在键盘所在的图层之上,而不是在弹窗出现的时候消失(如图1)。系统弹窗出现的时候会使键盘暂时不显示,而这种效果显然不符合要求的,由于没想到更好的办法,只好从键盘自身的UIKeyboard做文章了,通过获取当前键盘的UIKeyboard,手动改变UIKeyboard的透明度来勉强实现需求效果(如图2)。
核心代码
//懒加载---自定义弹窗
-(ShowForgetView*)showForgetView{
if(_showForgetView == nil)
{
_showForgetView = [[ShowForgetView alloc] initWithFrame:CGRectMake(
(FULL_WIDTH - 246)/2, (FUll_HEIGHT - 100)/2,246,173)];
_showForgetView.alpha = 0;
}
return _showForgetView;
}
-(void)clickfogotBtn:(id)sender{
//1.添加弹窗
UIWindow*keyWindow = [[UIApplicationsharedApplication].windows lastObject];
[keyWindow addSubview:self.showForgetView];
//2.获取当前键盘的keyboard
UIView*keyboard = [self getKeyBoard];
//3.设置动画效果
[UIView animateWithDuration:.3 animations:^{
self.showForgetView.alpha = 1;
self.view.alpha = 0.8;
keyboard.alpha = 0.3;
}];
}
//获取键盘UIKeyboard
- (UIView *)getKeyBoard
{
UIView *keyBoardView = nil;
NSArray *windows = [[UIApplication sharedApplication]windows];
for (UIWindow*window in [windows reverseObjectEnumerator])
{
keyBoardView = [self getKeyBoardInView:window];
if (keyBoardView)
{
return keyBoardView;
}
}
return nil;
}
- (UIView *)getKeyBoardInView:(UIView *)view
{
for(UIView *subView in [view subviews])
{
if (strstr(object_getClassName(subView), "UIKeyboard"))
{
return subView;
}else{
UIView *tempView = [selfgetKeyboardInView:subView];
if (tempView)
{
return tempView;
}
}
}
return nil;
}