当我更新contentInset时,UIScrollView会更改contentOffset

时间:2018-07-17 12:29:23

标签: ios swift uiscrollview

UIScrollView固定在控制器的视图边缘。

UIKeyboardWillShow上,我通过增加键盘高度来增加contentInsets的底值。动画制作完成后,内容将向上滚动一点。

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0.3
    self.formContainer.contentInset = UIEdgeInsets(top: self.rowPadding, left: 0, bottom: keyboardSize.height + self.navigator.frame.height + 10, right: 0)
    self.navigatorBottom.constant = -keyboardSize.height
    UIView.animate(withDuration: duration) {
        self.view.layoutIfNeeded()
    }
}

是否仍然在此处禁用内容滚动?

1 个答案:

答案 0 :(得分:0)

在开始动画之前保存contentOffset中的UIScrollView,并在动画contentOffset中将保存的值设置为completionHandler

看起来像这样:

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
    let duration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double ?? 0.3
    let currentContentOffset = self.formContainer.contentOffset.y //assume your scrollView scrolls vertically
    self.formContainer.contentInset = UIEdgeInsets(top: self.rowPadding, left: 0, bottom: keyboardSize.height + self.navigator.frame.height + 10, right: 0)
    self.navigatorBottom.constant = -keyboardSize.height
    UIView.animate(withDuration: duration, animations: {
        self.view.layoutIfNeeded()
    }) { _ in
        self.formContainer.contentOffset.y = currentContentOffset
    }
}