背景图像视图的高度未根据文本进行调整

时间:2019-02-08 10:10:58

标签: ios swift

我正在一对一聊天。因此,我要发送的文本在表格视图的背景下以灰色显示在图像视图的背景中。宽度合适,但高度没有根据文本进行调整...

有一个名为configure cell的函数,该函数每次都会被调用。

  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))")
      }
    }
  }

函数roundcorners的给出方式为

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()
  }

如果消息的高度更大,则消息将被裁剪... enter image description here

这里出了什么问题..?

1 个答案:

答案 0 :(得分:0)

您在使用这个

tableView.rowHeight = UITableView.automaticDimension

此外,请确保将背景视图固定在单元格的底部。下面是一个粗略的示例,不确定您的单元格布局是什么样。 enter image description here

这里还有一个教程,展示了如何自动调整UITableViewCell的大小 https://www.raywenderlich.com/8549-self-sizing-table-view-cells

希望有帮助。

相关问题