Keyboard

1.监听键盘出现、消失以更改view位置

- (void)keyboardWillShow:(NSNotification *)notification {
    CGFloat keyboardHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;//获取键盘高度
    CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height;
    CGRect customHousePriceViewRect = self.customHousePriceView.frame;
    
    CGFloat offset = customHousePriceViewRect.origin.y + self.view.frame.origin.y - (screenHeight - keyboardHeight - customHousePriceViewRect.size.height - 64);//偏移距离 64是状态栏和导航栏的高度和(20 + 44)
    double animateTime = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];//键盘出现的动画时间
    
    if (offset > 0) {
        [UIView animateWithDuration:animateTime animations:^{
            self.customHousePriceView.frame = CGRectMake(0, customHousePriceViewRect.origin.y-offset, customHousePriceViewRect.size.width, customHousePriceViewRect.size.height);
        }];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    double animateTime = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];//键盘出现的动画时间
    [UIView animateWithDuration:animateTime animations:^{
        [self loadFrameOfSubViews];
    }];
}

你可能感兴趣的:(Keyboard)