【问题标题】:Setting content inset makes UItableView scroll a little bit设置内容插入使 UItableView 滚动一点
【发布时间】:2017-03-12 15:24:06
【问题描述】:

我有一个 UITableView,底部有一个“添加新项目”单元格。当用户点击文本字段时,键盘会出现,并且 tableview 会获得内容插入,因此它的底部始终位于键盘上方而不是隐藏在键盘下方。这已经几乎完美了,但是在键盘出现后,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
}

这里还有一些截图:

After keyboard appeared

How it should look

【问题讨论】:

    标签: ios swift uitableview keyboard


    【解决方案1】:
        let indexPath = IndexPath(row: 0, section: tableView.numberOfSections - 1)
        tableView.scrollToRow(at: indexPath, at: .top, animated: false)
    

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

    【讨论】:

    • 谢谢!
    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    相关资源
    最近更新 更多