如何根据它更改视图高度是最近添加的子视图



我在尝试创建一个简单的信使时遇到了这样的问题。这是我来的:

  1. class ChatBubleUIView该类负责创建带有标签的气泡视图。它工作正常,根据标签高度计算视图高度

  2. 在我的单元格中,我创建了一个内容视图。在单元格类中,我将添加新的ChatBubleUIView实例作为内容视图的子视图。

  3. 问题是,内容无法扩展到我的ChatBubleInstance大小。

    class ChatMessageTableViewCell: UITableViewCell, MessageCellConfiguration {
    var message: Message?
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    func configureCell() {
            let chatBubleView = ChatBubleUIView(message: message!)
            self.addSubview(chatBubleView)
    } 
    }
    
  4. 在我的表中查看委托

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "messageCell") as! ChatMessageTableViewCell
     let data = currentUser.mesaageHistory[indexPath.row]
    cell.message = data
    cell.configureCell()
    return cell
    }
    
  5. 我还为我的表设置了估计的行高视图

     messageTableView.rowHeight = UITableViewAutomaticDimension
     messageTableView.estimatedRowHeight = 44
    

我应该怎么做才能设置为我的表视图行高聊天视图BubleUIView 实例高度。以前,我使用老式方法解决了这个问题,以编程方式确定chatViewBubleUIView实例高度,然后实现heightForRowAtIndexPath。但我想用AutoLayoaut来做到这一点。

设置标签的四个约束,例如top,bottom,leading,trailing标签的高度数应0。 然后它会根据内容自动增加其高度。如果您在任何视图中使用此标签,则视图的约束应与我上面提到的标签相同!

相关内容

最新更新