Swift 3 - 如何使UICollectionViewCell与label子视图的宽度相同

时间:2016-12-13 00:23:28

标签: swift uicollectionviewcell

如何使UICollectionViewCell使用单元格中包含的标签调整其宽度?

我已尝试过十几种方法让单元格调整其宽度到标签,但没有一种方法有效。

我认真尝试了很多不同的方法来跟踪它。有没有人有一个关于如何使用其中包含的标签宽度UICollectionViewCells进行大小调整的示例?

我已将UICollectionViewDelegateFlowLayout设为委托,并添加了

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

    //Adding text to the label inside of the cell here

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TagsCollectionViewCell

    //This is called before the creation of the cell

    return CGSize(width: cell.tagLabel.intrinsicContentSize.width, height: cell.tagLabel.intrinsicContentSize.height)
}

0 个答案:

没有答案
相关问题