【发布时间】:2018-05-19 13:16:48
【问题描述】:
我的表格视图最底部的单元格是一个带有文本字段的单元格。当用户点击它时,我想滚动它以使单元格位于键盘正上方。
当我用animated false 调用scrollRectToVisible(...) 时,一切都按预期工作,但是当animated 设置为true 时,表格只滚动单元格到目前为止,文本字段的底部是正确的键盘上方(见左图)。然而 bottonInsets 应该是正确的,因为我可以手动滚动单元格的最后一位,并且单元格位于正确的位置 (见右图)。
我认为在键盘上方滚动文本字段底部边缘的表格视图是表格视图的默认行为,但恐怕我不知道为什么当我想要动画时它似乎覆盖了我自己的滚动。
左图:
键盘正上方的 textFields 底部边缘(我保留了边框样式,以便您看得更清楚)。
右图: 我多么想要它。单元格的底部边缘位于键盘正上方。
func repositionTextfieldCell(in tableView: UITableView) {
guard let textFieldCell = tableView.bottommostCell() else { return }
guard let keyboardRect = activeKeyboardRect else { return }
// - Adjust insets
var bottomInset = keyboardRect.size.height
tableView.contentInset.bottom = bottomInset
tableView.scrollIndicatorInsets.bottom = bottomInset
// - Make cell visible
let x = textFieldCell.frame.minX
let y = textFieldCell.frame.maxY
tableView.scrollRectToVisible(CGRect(origin: CGPoint(x: x, y: y),
size: CGSize(width: 1, height: 1)), animated: true)
}
【问题讨论】:
标签: swift uitableview uiscrollview uitextfield swift4