3

セル内のテキストに基づいてセルが自動サイズ変更される動的 UICollectionView を作成しようとしています。しかし、何らかの理由で、カスタム UICollectionViewCell が全幅に拡張されません。私はAutoLayoutとして SnapKit を使用しており、すべてのビューはコードベースです。xib やストーリーボードはありません。現時点で取得したもののデバッグビューは次のとおりです。

ここに画像の説明を入力

セルを全幅に拡張し、高さをコンテンツが何であれ収まるようにします。ここに私のUICollectionViewControllerのスニペットがあります

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Home"

    let layout = UICollectionViewFlowLayout()
    layout.itemSize = CGSize(width: view.frame.width, height: view.frame.height)
    layout.scrollDirection = .vertical
    layout.estimatedItemSize = CGSize(width: 375, height: 250)

    collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), collectionViewLayout: layout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.backgroundColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)
    collectionView.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    self.view.addSubview(collectionView)

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    // Configure the cell
    if let c = cell as? HomeCollectionViewCell {
        c.contentView.frame = c.bounds
        c.contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        configureCell(c, indexPath: indexPath)

    }

    return cell
}

func configureCell(_ cell: HomeCollectionViewCell, indexPath: IndexPath) {
    cell.setText(withTitle: items[(indexPath as NSIndexPath).section].title)
}

そして、これが私のカスタムUICollectionViewCellのスニペットです

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

    self.contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    self.contentView.translatesAutoresizingMaskIntoConstraints = true
    self.contentView.bounds = CGRect(x: 0, y: 0, width: 99999, height: 99999)

    createTitle()

}

private func createTitle() {
    titleView = TTTAttributedLabel(frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: 56))
    titleView.tag = 1
    titleView.numberOfLines = 2
    titleView.delegate = self
    titleView.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(titleView)

    titleView.snp_updateConstraints(closure: {
        make in

        make.leading.trailing.equalTo(contentView).offset(10)
        make.top.equalTo(contentView).offset(10)
        make.bottom.equalTo(contentView).offset(-10)

    })
}

func setText(withTitle title:String, paragraph: String, image: String) {
    let titleAttributed = AttributedString(string: title, attributes: titleStringAttr)

    titleView.attributedText = titleAttributed
    titleView.sizeToFit()
}

私はこれに3日間取り組んできました..どんなアドバイスも大歓迎です!

4

1 に答える 1

0

それは実際には解決策ではありませんTTTAttributedLabelが、AutoLayout が機能しない原因となるブラック マジックを実行します。だから私にとっては、に変更したTTTAttributedLabelところUILabel、うまくいきました。

参考までに、SnapKit Githubの問題に同様の質問を投稿しました。ヒントは robertjpayne の功績によるものです ( https://github.com/SnapKit/SnapKit/issues/261 )

于 2016-09-13T14:59:40.997 に答える