ios - UIScrollView 在我更新 contentInset 时更改 contentOffset
问题描述
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()
}
}
无论如何在这里禁用内容滚动?
解决方案
在开始动画之前保存并将保存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
}
}
推荐阅读
- r - RTVS:无法使用 data.table 编织文档
- swift - 从 Firebase 获取用户名:在展开可选值时意外发现 nil
- ios - 如何将 UIImage 附加到数组“无法转换 'UIImageView 类型的值!”
- lua - Luci cbi - 如何在 ListValue 中获取部分名称
- powershell - 文本文件的内容被 Powershell 中的先前内容覆盖
- javascript - .url 文件使用什么标记语言?
- javascript - 如何在material-ui中导入more-vert图标以进行反应?
- dart - Flutter Dismissible 不工作
- ios - 如何在所有设备中快速布局每行三个图像
- jsf - p:spinner 应该只取数值