Keyboard

作者: 墓园派对 | 来源:发表于2018-09-07 10:58 被阅读0次

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

      本文链接:https://www.haomeiwen.com/subject/eloqgftx.html