点击UITextview键盘弹出时,上移vc.view,出现的问题.

点击UITextView键盘弹出时,上移vc.view,出现的问题.

之前为防止键盘挡住UITextView,我是这么做的:
先注册了键盘的相关通知:

self.myTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 450, 200, 44)];//这里特意让UITextView处于屏幕右下角,这样如果self.view不上移,肯定会被键盘挡住的
self.myTextView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.myTextView];
    
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后在键盘通知响应方法中处理

//键盘将要出现
- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
    NSLog(@"键盘即将出现");
    NSValue *value = [[paramNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];//使用UIKeyboardFrameBeginUserInfoKey,会出现切换输入法时获取的搜狗键盘不对.
    CGRect keyboardRect = [value CGRectValue];
    CGFloat keyboardH = keyboardRect.size.height;
    NSLog(@"键盘高度:%f", keyboardH);
    self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardH);
}

//键盘将要隐藏
- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
    NSLog(@"键盘即将隐藏");
    self.view.transform = CGAffineTransformIdentity;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    
    [self.view endEditing:YES];
}

这种办法,在VC没有navigationBar时没有什么bug.
但是当VC带有navigationBar时,就出问题了(就我现在这个demo来说).

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    self.window.backgroundColor = [UIColor whiteColor];
    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    nav.navigationBar.translucent = NO;
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    return YES;
}

当点击UITextView键盘弹出后,按下home键,然后再进入应用,就会看到UITextView不在键盘上方了,且点击屏幕让UITextView失去焦点后会看到顶端有一个黑色的view,目测应该是window.
那么如何解决呢?我这里只是写死坐标了.

//键盘将要显现
- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
    NSLog(@"键盘即将出现");
    NSValue *value = [[paramNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];//使用UIKeyboardFrameBeginUserInfoKey,会出现切换输入法时获取的搜狗键盘不对.
    CGRect keyboardRect = [value CGRectValue];
    CGFloat keyboardH = keyboardRect.size.height;
    NSLog(@"键盘高度:%f", keyboardH);
    
    CGRect r = self.view.frame;
    r.origin.y = 64 - keyboardH;
    self.view.frame = r;
}

//键盘将要隐藏
- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
    NSLog(@"键盘即将隐藏");
    
    CGRect r = self.view.frame;
    r.origin.y = 64;
    self.view.frame = r;
}

以为大功告成了,然而bug总是不期而遇,上面的解决办法在iOS9上没问题了,但是在iOS8上又有问题,当点击UITextView键盘弹出后,按下home键,然后再进入应用,会发现键盘还在但键盘上方的UITextView又不见了,但点击屏幕让UITextView失去焦点后,才会再看到UITextView.
那么如何解决呢?暂时没想到有啥好办法.目前的办法是让它在进入后台时失去焦点.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

- (void)applicationDidEnterBackground:(NSNotification *)paramNotification
{
    NSLog(@"应用已经进入后台,%@", paramNotification);
    [self.myTextView resignFirstResponder];
}

小伙伴们有更好的办法欢迎留言.

推荐阅读更多精彩内容