根据键盘调整视图高度

来源:互联网 发布:win7网络控制器有叹号 编辑:程序博客网 时间:2024/05/16 12:32

[[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];


#pragma mark - keybord(监测键盘响应方法)

- (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];

    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    NSNumber *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration = [animationDurationValue floatValue];

    [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];

}

- (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];

}

#pragma mark - frameChange(根据键盘弹出改变frame)

- (void)moveInputBarWithKeyboardHeight:(float)keyboardHeight withDuration:(NSTimeInterval)animationDuration

{

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

        _tableView.frame = CGRectMake(064SCREEN_WIDTHself.view.bounds.size.height -keyboardHeight - 44 - 64);

    } completion:^(BOOL finished) {

    }];

}


0 0