IOS8下利用自动布局实现键盘的弹出效果
2015-01-04 09:46
609 查看
-(void)keyboardChange:(NSNotification*)notice { NSValue *value = [[notice userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"]; float keyEnd_y = [value CGRectValue].origin.y; float animationDuration = [[notice userInfo][@"UIKeyboardAnimationDurationUserInfoKey"] floatValue]; CGRect frame = [[UIScreen mainScreen]bounds];//屏幕尺寸 int SCREEN_WIDTH = frame.size.width; int SCREEN_HEIGHT = frame.size.height; if(!shouldResignFirstResponder){ UIView *alphaCoverView = [[[UIApplication sharedApplication].delegate window] viewWithTag:10001]; if (!alphaCoverView) { alphaCoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, keyEnd_y - 40)]; [alphaCoverView setBackgroundColor:[UIColor blackColor]]; alphaCoverView.alpha = 0.0; alphaCoverView.tag = 10001; UITapGestureRecognizer *tapToResignirstResponder = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textFielfWillResignFirstResponder)]; [alphaCoverView addGestureRecognizer:tapToResignirstResponder]; [[[UIApplication sharedApplication].delegate window] addSubview:alphaCoverView]; } [UIView animateWithDuration:animationDuration animations:^{ alphaCoverView.alpha = 0.4; alphaCoverView.frame = CGRectMake(0, 0, SCREEN_WIDTH, keyEnd_y - 40);//40是编辑框视图的高 }]; } float constant = 0.0; if (shouldResignFirstResponder) { //收回键盘时,将约束的值复原 constant = SCREEN_HEIGHT - keyEnd_y; [UIView animateWithDuration:animationDuration animations:^{ _m_spaceToBottom.constant = constant; [self.view layoutIfNeeded]; }]; shouldResignFirstResponder = NO; }else{ constant = SCREEN_HEIGHT - keyEnd_y-46;//46是编辑框距离屏幕底部的距离 [UIView animateWithDuration:animationDuration animations:^{ _m_spaceToBottom.constant = constant; [self.view layoutIfNeeded]; }]; } } - (void)textFielfWillResignFirstResponder{ UIView *view = [[[UIApplication sharedApplication].delegate window] viewWithTag:10001]; [view removeFromSuperview]; shouldResignFirstResponder = YES; [self.view endEditing:YES]; }
//键盘变化监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:@"UIKeyboardWillChangeFrameNotification" object:nil];
借鉴文章:
http://blog.csdn.net/u013016828/article/details/42102411
相关文章推荐
- IOS8下利用自动布局实现键盘的弹出效果
- AJ学IOS(18)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- 通过修改自动布局的约束实现键盘弹出后view上推
- 猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- android中隐藏与显示软键盘及不自动弹出键盘及软键盘将布局顶上去的实现方法
- (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- 猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- 自定义ViewGroup实现水平布局空间不足自动换行的效果
- 利用jwplayer的API实现播放定时弹出框效果
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- 利用ScrollView实现布局自动滚动
- 简单利用Dialog实现Ios从底部弹出的效果,合QQ空间里面的发表说说弹出拍照的效果类似
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- 利用JS实现简单的弹出窗品效果
- Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入。 那么,弹出软键盘后必然会造成原有布局高度的减少,那么系统应该如何来处理布局的减少
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- Dom利用javascript实现模拟弹出对话框效果的代码,类似JQuery中的dialog!
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法
- 解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法