繁体   English   中英

设置内容插入使UItableView滚动一点

[英]Setting content inset makes UItableView scroll a little bit

我有一个UITableView,底部有一个“添加新项”单元格。 当用户点击文本字段时,会出现键盘并且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)

添加这两行我的功能部分修复了它。 我可以发誓,我以前试过这个,但我不认为我使用的是非动画版本。 也许这会有所不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM