首页 > 解决方案 > UICollectionViewCell 不是圆角

问题描述

我试图使角落变圆,但是当我使用cornerRadius和maskToBouds时,它不起作用

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
        cell.contentView.layer.cornerRadius = 30
        cell.contentView.layer.masksToBounds = true
        return cell
    }

细胞: 图 1

标签: iosswiftxcodeuicollectionviewcellcornerradius

解决方案


尝试在willDisplay:forItemAt:

例如

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        cell.layer.cornerRadius = 30
        cell.clipsToBounds = true
    }

推荐阅读