处理键盘遮挡的思路整理

之前处理过键盘遮挡的问题,整体来看,要想在新的页面里做相同的处理,比较繁琐。
先说一下之前是怎么做的:viewcontroller --> tableview --> tableviewcell --> textview/textfield(视图层次)
处理起来是通过tableviewcell的代理是viewcontroller将textview或textfield的编辑开始结束事件传递给viewcontroller,然后viewcontroller通过响应开始和结束编辑事件并且监听键盘的show和hidden的通知。来移动整个view以避免遮挡。
这样处理的确实现的避免键盘遮挡当前录入内容。不过其他的页面移植这个功能很麻烦。
下面是相关的代码:

#pragma mark -- 处理键盘遮挡 -- TextView或textfield开始编辑 - (void)textFieldBegin:(UIView *)sender {__weak typeof(self) weakSelf = self; CGRect rect = [sender convertRect:sender.bounds toView:[[UIApplication sharedApplication] keyWindow]]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{if (self.view.frame.size.height - rect.origin.y - rect.size.height < self.keyHeight) {NSLog(@"keybord is keep out inputview"); [UIView animateWithDuration:0.1 animations:^{weakSelf.view.center = CGPointMake(weakSelf.view.center.x, weakSelf.view.center.y - weakSelf.keyHeight); self.keyBgView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, self.keyHeight)]; self.keyBgView.backgroundColor = [UIColor whiteColor]; [weakSelf.view addSubview:self.keyBgView]; self.keyDo = YES; }]; }}); }- (void)keyBordDown {__weak typeof(self) weakSelf = self; if (self.keyDo) { [UIView animateWithDuration:0.1 animations:^{weakSelf.view.center = CGPointMake(weakSelf.view.center.x, weakSelf.view.center.y + weakSelf.keyHeight); self.keyBgView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, self.keyHeight)]; self.keyBgView.backgroundColor = [UIColor whiteColor]; [weakSelf.view addSubview:self.keyBgView]; self.keyDo = NO; }]; } }-(void)KeyboardWillShow:(NSNotification *)notification {NSDictionary *info = [notification userInfo]; //获取高度 NSValue *value = https://www.it610.com/article/[info objectForKey:@"UIKeyboardBoundsUserInfoKey"]; //关键的一句,网上关于获取键盘高度的解决办法,多到这句就over了。系统宏定义的UIKeyboardBoundsUserInfoKey等测试都不能获取正确的值。不知道为什么。。。CGSize keyboardSize = [value CGRectValue].size; //NSLog(@"横屏%f",keyboardSize.height); float keyboardHeight = keyboardSize.height; self.keyHeight = keyboardHeight; NSLog(@"%f",keyboardHeight + 30); }- (void)KeyboardWillHidden:(NSNotificationCenter *)notification {[self keyBordDown]; }

但是,处理键盘遮挡,完全没有必要这样来处理。
监听到键盘的show和hidden的通知,就足处理键盘遮挡的问题。
关键在于在键盘show的时候获取到当前的第一响应者
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; ///第一响应这 UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

【处理键盘遮挡的思路整理】如此,即可通过以下代码来做处理,而且方便移植到新的vc
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil]; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[[[UIApplication sharedApplication] keyWindow] endEditing:YES]; }#pragma mark -- 处理键盘遮挡 -(void)KeyboardWillShow:(NSNotification *)notification {UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow]; UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)]; CGRect rect = [firstResponder convertRect:firstResponder.frame toView:self.view]; //获取相对于self.view的位置 NSDictionary *userInfo = [notification userInfo]; NSValue* aValue = https://www.it610.com/article/[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; //获取弹出键盘的fame的value值 CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window]; //获取键盘相对于self.view的frame ,传window和传nil是一样的 CGFloat keyboardTop = keyboardRect.origin.y; NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; //获取键盘弹出动画时间值 NSTimeInterval animationDuration = [animationDurationValue doubleValue]; if (keyboardTop < CGRectGetMaxY(rect)) {//如果键盘盖住了输入框 CGFloat gap = keyboardTop - CGRectGetMaxY(rect) - 10; //计算需要网上移动的偏移量(输入框底部离键盘顶部为10的间距) __weak typeof(self)weakSelf = self; [UIView animateWithDuration:animationDuration animations:^{ weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, gap, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height); }]; } }- (void)KeyboardWillHidden:(NSNotification *)notification {NSDictionary *userInfo = [notification userInfo]; NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; //获取键盘隐藏动画时间值 NSTimeInterval animationDuration = [animationDurationValue doubleValue]; if (self.view.frame.origin.y < 0) {//如果有偏移,当影藏键盘的时候就复原 __weak typeof(self)weakSelf = self; [UIView animateWithDuration:animationDuration animations:^{ weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height); }]; } }

    推荐阅读