少しチャットメッセージアプリケーションを作成しています。その目的のために私はコレクションビューを使用しています。私はたくさんのコードを見つけ出し、それをまとめてそれを有効にしました。しかし今、私は時間のカスタムセルにいくつかの追加のラベルを追加しようと - とグループチャット - ユーザー名。しかし、私はバブルビューにラベルを追加できないようです。 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)
}
は、あなたが一緒に貼り付けコードを理解おおよそのサイズに
estimatedItemSize
を設定します。 –これはそれをよりよく理解しようとする試みです。私が言ったように、それはちょうど推定された高さの関数です、それは私には完全には分かりません。私はあなたがなぜそれをしなければならないのか理解しており、結果として私はそれが何をしているのかを知っています。しかし、この関数の出力を変更しようとすると失敗しました。 – AlexVilla147