2017-09-23 5 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方法だと思います。しかし、正直言って私はその方法を変更する方法がわからないので、現時点ではテキストビューで動作します。

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

は、あなたが一緒に貼り付けコードを理解おおよそのサイズにestimatedItemSizeを設定します。 –

+0

これはそれをよりよく理解しようとする試みです。私が言ったように、それはちょうど推定された高さの関数です、それは私には完全には分かりません。私はあなたがなぜそれをしなければならないのか理解しており、結果として私はそれが何をしているのかを知っています。しかし、この関数の出力を変更しようとすると失敗しました。 – AlexVilla147

答えて

0

試してみてください。

  1. UICollectionViewCellサブクラスでは、確実に、セルでは、セルに直接
  2. contentViewにサブビューを追加していないことの制約サブビューはcontentViewになります
  3. UICollectionViewFlowLayoutを使用している場合は、itemSize = UICollectionViewFlowLayoutAutomaticSize
  4. あなたがUICollectionViewFlowLayoutを使用している場合は、
+0

おかげさまで、私は試しましたが、このエラーが表示されました: 'UICollectionViewLayoutAttributes:-setFrame:CGRectNullが定義されていません。' – AlexVilla147

+0

ポイント3とポイント4の違いは何ですか?私はちょうどポイント3をオーバーライドメソッドsizeforitemAtで使用してitemSizeを返す – AlexVilla147

+0

私は最終的にすべての点を理解していますが、IDは問題を解決しませんでした。 timeViewはバブルビューの下にあり、私は本当に不満を感じます。そして、突然、1つの線に複数のセルが互いに切断されています。 – AlexVilla147

関連する問題