项目中有很多输入框,有输入框就会唤出键盘,唤出键盘就很经常键盘挡住输入框。如何解决这个问题?
这个问题并不难,相信大家都会写。但是写多了大家就会发现,项目中这么多页面有输入框的,那就意味着很多地方都要写,而且写的代码都差不多。那有没有方便点的,全局控制控制器的view跟随键盘起伏,或者调用一句代码就可以搞定的?
针对这个问题,为此我实现了一句话设置当前控制器的view跟随键盘起伏。
代码不多只有轻量级的两个类:
看看效果:
实现思路 主要代码实现:
1、监听输入框开始编辑,键盘改变的通知
// TextField开始编辑
[[NSNotificationCenter defaultCenter] addObserver:instance selector:@selector(textFieldTextDidBeginEditingNotification:) name:UITextFieldTextDidBeginEditingNotification
object:nil];
// TextView开始编辑
[[NSNotificationCenter defaultCenter] addObserver:instance selector:@selector(textViewTextDidBeginEditingNotification:) name:UITextViewTextDidBeginEditingNotification
object:nil];
// 键盘将要改变
[[NSNotificationCenter defaultCenter] addObserver:instance selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
2、获取当前的控制器,判断键盘是否挡住输入框,然后计算偏移view
/// 偏移View
-(void)transformView:(NSNotification *)notification {
//获取键盘弹出后的Rect
NSValue *keyBoardEndBounds=[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect endRect=[keyBoardEndBounds CGRectValue];
UIView *targetView;
if ([self.inputObject isKindOfClass:[UIView class]]) {
targetView = self.inputObject;
}
if (!targetView) {
return;
}
UIViewController *vc = [self getCurrentVC];
// targetView 在 toView 的 convertRect位置上的位置
CGRect targetRect = [targetView convertRect:CGRectMake(0, 0, targetView.frame.size.width, targetView.frame.size.height) toView:vc.view];
CGFloat height = [UIScreen mainScreen].bounds.size.height;
CGFloat maxY = CGRectGetMaxY(targetRect);
CGFloat cha = endRect.origin.y - maxY;
if (maxY > endRect.origin.y) { // 键盘会挡住输入框
if ([vc.view.subviews.firstObject isKindOfClass:[UIScrollView class]]) {// 如果self.view是UIScrollView,则滚动contentOffset
UIScrollView *scrollView= vc.view.subviews.firstObject;
//NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y + (-cha) + 10);
}else {
CGFloat interval = 0;
if (cha < -10) {
interval = 10;
}
[UIView animateWithDuration:0.25f animations:^{
[vc.view setFrame:CGRectMake(vc.view.frame.origin.x, cha - interval, vc.view.frame.size.width, vc.view.frame.size.height)];
}];
}
}
if (endRect.origin.y == height) { //键盘回收 deltaY>100
//NSLog(@"键盘回收");
if ([vc.view.subviews.firstObject isKindOfClass:[UIScrollView class]]) {
//UIScrollView *scrollView= self.view.subviews.firstObject;
//NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}else {
[UIView animateWithDuration:0.25f animations:^{
[vc.view setFrame:CGRectMake(0, 0, vc.view.frame.size.width, vc.view.frame.size.height)];
}];
self.inputObject = nil;
self.keyboardNotifivation = nil;
}
}
}
使用方式有两种:
第一种:只导入JHKeyBoardManager.h这个类到项目中,在AppDelegate写一句代码即可
#import "JHKeyBoardManager.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[JHKeyBoardManager sharedManager];
return YES;
}
第二种:只导入UIViewController+KeyBoardManager.h这个类到项目中,要用的控制器只需掉用一个方法即可
#import "UIViewController+KeyBoardManager.h"
- (void)viewDidLoad {
[super viewDidLoad];
[self transformViewForKeyboard];
}
demo地址:https://github.com/iOSHJH/JHKeyBoardManager
有问题欢迎issues,若项目对你有用还望不吝给个star让我动力十足😀