我正在进行一对一聊天。因此,我发送的文本显示在灰色 ImageView 背景的表格 View 上。宽度是正确的,但高度没有根据文本调整...
有一个称为配置单元的函数,每次都会调用..
func configureCell(_ message: ChatMessage) {
self.roundCorners(corners: [.topLeft, .bottomLeft, .bottomRight], radius: CGFloat(Constants.Chat.corenerRadius))
print("messageBackground.awakeFromNib: \(messageBackground.bounds)")
messageTextLabel.text = message.text?.text
messageTimeLabel.text = message.information?.composed?.toString(withFormat: Constants.Chat.timeFormat)
if let image = message.information?.status.image {
messageStatus.image = image
if (message.text?.text?.contains("offline"))! {
print("message.information?.status: \(String(describing: message.information?.status.rawValue))")
}
}
}
函数圆角
给出为,
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
messageBackground.layer.mask = mask
messageBackground.clipsToBounds = true
messageBackground.layer.shouldRasterize = true
messageBackground.layer.masksToBounds = true
messageBackground.sizeToFit()
}
这里出了什么问题......?
最佳答案
你用这个吗
tableView.rowHeight = UITableView.automaticDimension
此外,请确保您的背景 View 固定在单元格的底部。下面是一个粗略的示例,不确定您的单元格布局是什么样的。
这里还有一个教程,展示如何自动调整 UITableViewCell 的大小 https://www.raywenderlich.com/8549-self-sizing-table-view-cells
希望有帮助。
关于ios - 背景 ImageView 的高度不根据文本调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54590091/