3

I'm trying to create a dynamic UICollectionView whose cells auto-resize based on the text inside it. But for some reasons, my custom UICollectionViewCell won't expand to the full width. I am using SnapKit as AutoLayout and all my views are code-based; no xib or storyboard. Here's a debug view of what I got at the moment:

enter image description here

I want the cell to expand full width and the height to fit whatever the content is. Here's a snippet on my 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)
}

And here's a snippet of my custom 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()
}

I've spent 3 days just working on this on and on.. Any advice appreciated!

yonasstephen
  • 2,645
  • 4
  • 23
  • 48
  • Have you tried implementing "- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath" ? – Tom Jul 26 '16 at 17:05
  • hmm I thought if I want the size to be dynamic I don't need to implement that? I tried that before and remove the `layout.estimatedItemSize` and all becomes the same size.. – yonasstephen Jul 27 '16 at 02:23
  • Did you ever arrive at an answer for this problem? Running into a very similar issue myself on a UITableViewCell. – Friendly King Sep 12 '16 at 22:48
  • 1
    Hey @JohnZ refer to the answer I just posted. Hope it helps! – yonasstephen Sep 13 '16 at 15:00
  • @yonasstephen Thank you! – Friendly King Sep 13 '16 at 15:18

1 Answers1

0

Well it's not really a solution, but the TTTAttributedLabel does some black magic that causes the AutoLayout not to work. So for me, I changed the TTTAttributedLabel to UILabel and it works fine.

FYI I posted similar question on SnapKit Github issues; credits to robertjpayne there for the hint (https://github.com/SnapKit/SnapKit/issues/261)

yonasstephen
  • 2,645
  • 4
  • 23
  • 48