ios - 背景 ImageView 的高度不根据文本调整

标签 ios swift

我正在进行一对一聊天。因此,我发送的文本显示在灰色 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()
  }

如果消息的高度更高,这就是它被剪裁的方式... enter image description here

这里出了什么问题......?

最佳答案

你用这个吗

tableView.rowHeight = UITableView.automaticDimension

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

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

希望有帮助。

关于ios - 背景 ImageView 的高度不根据文本调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54590091/

相关文章:

ios - 如何获取数组(或某些数据结构?)中的 Assets.xcassets 文件名

ios - Swift:Apple Mach-O 链接器错误(文件对于架构 x86_64 而言太小)

javascript - 在 iOS Swift 中处理 Javascript 文件下载

ios - 在 UITextfield 上订阅(控制事件,编辑已更改)

ios - 使用 UIStepper 进行 UILocalNotification 的时间

ios - AVPlayer 播放更新标签会导致问题

ios - Swift UIView Constraint Constant Height Changing时间获取LayoutConstraints警告

ios - 在 UIViewRepresentable 中获取 UIView 的框架或边界

ios - MKCoordinatespan 根据源和目的地中心坐标计算 latitudedelta 和 longitudedelta

ios - 绘制(_矩形: CGRect) not getting called in custom view