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

时间:2016-10-30 07:50:16

标签: ios swift uitableview keyboard

我有一个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
}

以下是一些截图:

merge

After keyboard appeared

1 个答案:

答案 0 :(得分:3)

    let indexPath = IndexPath(row: 0, section: tableView.numberOfSections - 1)
    tableView.scrollToRow(at: indexPath, at: .top, animated: false)

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

相关问题