快速在集合视图的单元格中添加阴影

时间:2018-10-28 14:59:15

标签: swift collections view

我正在尝试创建带有圆角和阴影的集合视图单元格。这是我的代码,此代码运行成功,但是集合视图单元格带有圆角,但没有阴影。我不知道出了什么问题?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let apiCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ApiCollectionViewCell", for: indexPath) as? ApiCollectionViewCell
        apiCollectionViewCell?.layer.cornerRadius = 25
        apiCollectionViewCell?.clipsToBounds = true
        apiCollectionViewCell?.contentView.layer.cornerRadius = 25
        apiCollectionViewCell?.contentView.layer.masksToBounds = true
        apiCollectionViewCell?.layer.shadowColor = UIColor.black.cgColor
        apiCollectionViewCell?.layer.shadowOffset = CGSize(width: 0, height: 2.0)
        apiCollectionViewCell?.layer.shadowRadius = 2
        apiCollectionViewCell?.layer.opacity = 1
        apiCollectionViewCell?.layer.masksToBounds = true
        apiCollectionViewCell?.layer.shadowPath = UIBezierPath(roundedRect:(apiCollectionViewCell?.bounds)!, cornerRadius:(apiCollectionViewCell?.contentView.layer.cornerRadius)!).cgPath
        return apiCollectionViewCell ?? UICollectionViewCell()
    }

1 个答案:

答案 0 :(得分:0)

您只需要更改一行代码:

apiCollectionViewCell?.clipsToBounds = false