2017-09-23 39 views
-1

我正在创建一个小聊天消息应用程序。为此,我使用集合视图。我发现了很多代码,并设法将它们结合在一起以使其工作。但是现在我尝试在自定义单元格中添加一些额外的标签,并在组聊天中使用用户名。但似乎我不能在泡泡视图中添加任何标签。 timeLabel位于气泡视图下方,尽管我以时间表应该位于textView下的气泡视图内的方式设置约束。无法在聊天应用程序的集合视图中调整单元格大小

下面是代码:

override init(frame: CGRect) { 
    super.init(frame: frame) 

    addSubview(bubbleView) 
    addSubview(textView) 
    addSubview(profileImageView) 
    addSubview(timeView) 
    addSubview(usernameView) 



    //x,y,w,h 
    profileImageView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true 
    // profileImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 
    profileImageView.widthAnchor.constraint(equalToConstant: 32).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 32).isActive = true 
    profileImageView.topAnchor.constraint(equalTo: bubbleView.topAnchor).isActive = true 

    //x,y,w,h 

    bubbleViewRightAnchor = bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8) 
    bubbleViewRightAnchor?.isActive = true 
    bubbleViewLeftAnchor = bubbleView.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8) 
    // bubbleViewLeftAnchor?.isActive = false 
    bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true 
    bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 200) 
    bubbleWidthAnchor?.isActive = true 
    bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true 

    //ios 9 constraints 
    //x,y,w,h 
    // textView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true 
    textView.leftAnchor.constraint(equalTo: bubbleView.leftAnchor, constant: 8).isActive = true 
    textView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true 
    textView.rightAnchor.constraint(equalTo: bubbleView.rightAnchor).isActive = true 
    // textView.widthAnchor.constraint(equalToConstant: 200).isActive = true 
    textView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true 
    textView.bottomAnchor.constraint(equalTo: timeView.topAnchor, constant: 8).isActive = true 

    timeView.rightAnchor.constraint(equalTo: textView.rightAnchor, constant: 8).isActive = true 
    //timeView.topAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 8).isActive = true 
    timeView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true 
    timeWidthAnchor = timeView.widthAnchor.constraint(equalToConstant: 40) 
    timeWidthAnchor?.isActive = true 

    /* usernameView.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true 
    usernameView.bottomAnchor.constraint(equalTo: textView.topAnchor).isActive = true 
    nameWidthAnchor = usernameView.widthAnchor.constraint(equalToConstant: 100) 
    nameWidthAnchor?.isActive = true 
    usernameView.heightAnchor.constraint(equalToConstant: 20) 
    nameHeightAnchor?.isActive = true */ 
} 

我认为这个问题是在集合视图的sizeForItemAt方法。但说实话,我不知道如何改变这种方法,以便它仍然可以像现在这样使用textview。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    var height: CGFloat = 80 
    let text: String? 
    //get estimated height somehow???? 
    if messages[indexPath.item].text != nil { 
     text = messages[indexPath.item].text 
     height = estimateFrameForText(text!).height + 20 
    } 

    let width = UIScreen.main.bounds.width 
    return CGSize(width: width, height: height) 
} 

fileprivate func estimateFrameForText(_ text: String) -> CGRect { 
    let size = CGSize(width: 200, height: 1000) 
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin) 
    return NSString(string: text).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], context: nil) 
} 

sample chat

+0

明白自己粘贴在一起的代码。 –

+0

这是尝试更好地理解它。正如我所说,这只是估计的身高功能,这对我来说并不完全清楚。我明白你为什么必须这样做,结果我知道它在做什么。但是我试图改变这个函数的输出时失败了。 – AlexVilla147

回答

0

尝试以下方法:

  1. UICollectionViewCell子类,子视图添加到contentView,而不是细胞直接
  2. 在细胞中,保证的约束子视图绑定到contentView
  3. 如果您使用的是UICollectionViewFlowLayout,则将itemSize = UICollectionViewFlowLayoutAutomaticSize
  4. 如果使用UICollectionViewFlowLayout,然后设置estimatedItemSize为近似尺寸
+0

嘿谢谢,我试过了,但是这个错误已经显示出来了:'UICollectionViewLayoutAttributes:-setFrame:with CGRectNull is undefined。' – AlexVilla147

+0

和点3和点4有什么不同?我刚刚在重写方法sizeforitemAt中使用了第3点,并且返回了itemSize – AlexVilla147

+0

我终于理解了所有的点,但是id没有解决问题。 timeView仍然在泡泡视图下方,这让我非常沮丧。突然间,一行中有多个单元格相互切割。 – AlexVilla147

相关问题