【发布时间】:2023-03-15 20:38:01
【问题描述】:
一个类似的问题是this,除了我没有估计的行高,我将实际高度存储到字典中,然后使用它们,或者使用UITableViewAutomaticDimension。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cellHeightDict[indexPath.row] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeightDict[indexPath.row] {
return height
} else {
return UITableViewAutomaticDimension
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cellHeightDict[indexPath.row] {
return height
} else {
return UITableViewAutomaticDimension
}
}
所以我现在正在用 Swift 4 制作一个消息传递应用程序。当用户显示键盘时,我希望消息随着键盘向上移动。所以为了做到这一点,我有一个变量keyboardHeight,它正确得到了键盘的高度:
let keyboardHeight: CGFloat = ((userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height)!
我将表格视图的高度更改为keyboardHeight:
self.messageTableViewHeight.constant -= keyboardHeight
因此用户可以看到所有的消息。为了通过键盘动画上移消息,我更改了 table view 的 contentOffset:
self.messageTableView.contentOffset.y += keyboardHeight
所有这些都在UIView.animationWithDuration 中,之后我调用self.view.layoutIfNeeded(),因此转换和一切正常。 但是,当我发送消息时没有滚动到底部,messageTableView 的内容会向下跳??? 而且它似乎向下移动了keyboardHeight,至少当我看着它。我正在使用messageTableView.reloadData()。这是一些图片。
它在“Hi”和“Testing”消息之间跳跃。
【问题讨论】:
-
对于文本输入位于最底部的聊天应用程序,通常您只需将视图控制器的整个视图向上移动键盘高度即可
-
我试过这样做,但是我如何让用户看到所有的消息?如果我将整个表格视图向上移动,那么它会在标题下方滑动。
-
我想把它向上移动,然后调整 contentInset,但是滚动条会滚动到视图之外,插入行也是一个问题。
标签: ios swift uitableview messaging