前言:虽然说目前也没多少APP输入框会被弹出的键盘遮住,但是遇到了也挺麻烦的,所以,我也简单的整理了一下,代码如下:

1574840914802133.gif
添加监听
#pragma mark - 键盘事件
- (void)initKeyboardObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidHide:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];
}
参数描述
#define MAINSCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define MAINSCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define FIRSTRESPONDERBOTTOM 20
//是否正在显示键盘
@property (nonatomic, assign) BOOL isKeyboardShow;
//是否第三方
@property (nonatomic, assign) BOOL isThirdPartKeyboard;
//次数
@property (nonatomic, assign) NSInteger keyboardShowTime;
//键盘动画时间
@property (nonatomic, assign) CGFloat keyboardAnimateDur;
//键盘位置
@property (nonatomic, assign) CGRect keyboardFrame;
//改变的高度
@property (nonatomic, assign) CGFloat changeHeight;
@property (nonatomic, assign) CGFloat oldY;
监听处理
//键盘即将显示
- (void)keyboardWillShow:(NSNotification*)notification
{
    
    NSValue* keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [keyboardBoundsValue CGRectValue];
    
    NSNumber* keyboardAnimationDur = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    float animationDur = [keyboardAnimationDur floatValue];
    
    _keyboardShowTime++;
    //第三方输入法有bug第一次弹出没有keyboardRect
    if (animationDur > 0.0f && keyboardRect.size.height == 0) {
        _isThirdPartKeyboard = YES;
    }
    //第三方输入法有动画间隔时没有高度
    if (_isThirdPartKeyboard) {
        // 第三次调用keyboardWillShow的时候键盘完全展开
        if (_keyboardShowTime == 3 && keyboardRect.size.height != 0 && keyboardRect.origin.y != 0) {
            _keyboardFrame = keyboardRect;
            [self changeForKeyBoradFrame:keyboardRect];
        }
        if (animationDur > 0.0) {
            _keyboardAnimateDur = animationDur;
        }
    } else {
        if (animationDur > 0.0) {
            _keyboardFrame = keyboardRect;
            _keyboardAnimateDur = animationDur;
            [self changeForKeyBoradFrame:keyboardRect];
        }
    }
}
//键盘已经显示
- (void)keyboardDidShow:(NSNotification*)notification {
    _isKeyboardShow = YES;
}
//键盘即将隐藏
- (void)keyboardWillHide:(NSNotification*)notification
{
    NSNumber* keyboardAnimationDur = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    float animationDur = [keyboardAnimationDur floatValue];
    
    _isThirdPartKeyboard = NO;
    _keyboardShowTime = 0;
    
    if (animationDur > 0.0 && self.changeHeight<0) {
        CGRect animationFrame = self.animationView.frame;
        animationFrame.origin.y -= self.changeHeight;
        [UIView animateWithDuration:_keyboardAnimateDur animations:^{
            self.animationView.frame = animationFrame;
            [self.animationView layoutIfNeeded];
            [self.animationView layoutSubviews];
        } completion:^(BOOL finished) {
            self.changeHeight = 0;
            self.firstResponderInWindowFrame = CGRectZero;
            self.keyboardFrame = CGRectZero;
        }];
    }
}
//键盘已经隐藏
- (void)keyboardDidHide:(NSNotification*)notification {
    _isKeyboardShow = NO;
}
- (void)changeForKeyBoradFrame:(CGRect)keyboardRect {
    //判断键盘是不是低于第一响应者+预设的空隙
    //计算:键盘的Y - 第一响应者的Y+高+离键盘的空隙 =需要移动的高度
    CGFloat deviation = keyboardRect.origin.y - (self.firstResponderInWindowFrame.origin.y +self.firstResponderInWindowFrame.size.height +FIRSTRESPONDERBOTTOM);
    if (deviation <0) {
        self.changeHeight = deviation;
        CGRect animatieViewFrame = self.animationView.frame;
        //防止多次计算导致位置错误
        if (self.oldY ==0) {
            self.oldY = animatieViewFrame.origin.y;
        }
        animatieViewFrame.origin.y =self.oldY+deviation;
        
        [UIView animateWithDuration:_keyboardAnimateDur animations:^{
            self.animationView.frame = animatieViewFrame;
            
            [self.animationView layoutIfNeeded];
            [self.animationView layoutSubviews];
        }completion:^(BOOL finished) {
            
        }];
    }
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
最后,调用我感觉也算简单了
需要传2个参数 一个是animationView,初始化的时候就可以传,另一个是firstResponder点击的视图,在点击的时候传,类似下面这样。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [HYKeyboardManager sharedManager].firstResponder =textField;
    return YES;
}