我正在创建一个聊天视图,并且正在尝试基于自动布局设计单元格。我的主要问题是,有一个图像视图,正下方有一个聊天文本标签,它显示多行,如果没有图像,则只显示文本,如果没有文本,则只显示图像。图像应该是单元格宽度的60%和高度的40%。以下是我的单元设计,为了清晰起见,我为每个视图添加了背景颜色。管理聊天视图的自动布局约束
但给予高度成正比,我不能以编程方式设置其高度。所以目前我尝试给图像视图固定的高度,并在代码中以编程方式进行管理。以下是我调整imageview和聊天文字高度的代码。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : ChatViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("chatCell", forIndexPath: indexPath) as! ChatViewCell
cell.fullNameLabel.text = tempArray[indexPath.row].0
cell.fullDateLabel.text = tempArray[indexPath.row].1
cell.chatSentStatusLabel.text = tempArray[indexPath.row].2
cell.chatTimeLabel.text = tempArray[indexPath.row].3
let chatBubbleData1 = tempArray[indexPath.row].4
if (chatBubbleData1.image == nil)
{
cell.chatImageHeightConstraint.constant = 0
}
else
{
cell.chatImageHeightConstraint.constant = 150
}
cell.chatTextContainerView?.layer.cornerRadius = 10.0
cell.chatTextContainerView?.layer.masksToBounds = true
cell.chatBubbleView?.layer.cornerRadius = 10.0
cell.chatBubbleView?.layer.masksToBounds = true
cell.chatImageView.image = chatBubbleData1.image
cell.userPointerView.colors = (0.0, 0.0, 1.0, 1.0)
cell.userPointerView.backgroundColor = .clearColor()
cell.chatImageView?.layer.cornerRadius = 10.0
cell.chatImageView?.layer.masksToBounds = true
if (chatBubbleData1.text?.characters.count > 0)
{
cell.chatText?.numberOfLines = 0 // Making it multiline
cell.chatText?.text = chatBubbleData1.text
cell.chatText?.sizeToFit()
cell.chatTextContainerHeightConstraint.constant = CGRectGetHeight(cell.chatText.frame)+20
}
else
{
cell.chatText?.text = ""
cell.chatTextContainerHeightConstraint.constant = 0
}
cell.setNeedsDisplay()
return cell
}
这是现在的结果
多行不似乎工作。我不确定我是否遵循最佳方法。如果有人有任何解决方案或任何建议,请让我知道。
PS:请不要建议像JSQMessagesViewController这样的第三方。我已经知道这些。我正在做这个作为学习的一部分。
您是否已为聊天标签设置固定高度限制? –
不,我只设置顶部,底部,领先和尾随其超级视角。 – Gamerlegend