我有一个 UITableView,底部有一个“添加新项目”单元格。当用户点击文本字段时,键盘会出现,并且表格 View 会获得内容插入,因此其底部始终位于键盘上方,而不是隐藏在键盘下方。这已经几乎完美地工作了,但是键盘出现后,tableview不再像以前那样滚动到底部,它稍微高于底部,因此底部单元格被键盘部分覆盖。
为了设置内容插图,我注册了键盘通知并运行以下代码:
func adjustForKeyboard(notification: Notification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
if notification.name == Notification.Name.UIKeyboardWillHide {
tableView.contentInset = UIEdgeInsets.zero
} else {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
tableView.scrollIndicatorInsets = tableView.contentInset
}
这里还有一些屏幕截图:
最佳答案
let indexPath = IndexPath(row: 0, section: tableView.numberOfSections - 1)
tableView.scrollToRow(at: indexPath, at: .top, animated: false)
将这两行添加到我的函数中部分地修复了它。我可以发誓,我以前尝试过这个,但我不认为我使用了非动画版本。也许这会有所不同。
关于ios - 设置内容插入使 UItableView 滚动一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40327027/