【发布时间】: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
}
这里还有一些截图:
【问题讨论】:
标签: ios swift uitableview keyboard