textField弹起键盘时,背景视图调整避免键盘遮挡

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

- (void)viewDidLoad{
...
// 1.通知中心添加观察者,监听键盘是否弹出
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}

// 2.键盘弹出时候,调整视图高度
-(void)keyboardWillAppear:(NSNotification *)notification{
    CGRect currentFrame = _themeTableView.frame;
    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    currentFrame.origin.y = currentFrame.origin.y - change ;
    _themeTableView.frame = currentFrame;
}
// 3.当键盘消失后,视图需要恢复原状。
-(void)keyboardWillDisappear:(NSNotification *)notification{
    CGRect currentFrame = _themeTableView.frame;
    CGFloat change = [self keyboardEndingFrameHeight:[notification userInfo]];
    currentFrame.origin.y = currentFrame.origin.y + change ;
    _themeTableView.frame = currentFrame;
}
// 4.计算键盘的高度
-(CGFloat)keyboardEndingFrameHeight:(NSDictionary *)userInfo{
    CGRect keyboardEndingUncorrectedFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGRect keyboardEndingFrame = [_themeTableView convertRect:keyboardEndingUncorrectedFrame fromView:nil];
    return keyboardEndingFrame.size.height;
}