从 xib 加载单元格时删除 UICollectionView 中的边框

remove border in UICollectionView when cells loaded from xib

我有一个 collectionView,我的单元格是从 xib 加载的。

我正在尝试使用以下几行删除边框(我只想要阴影):

cell.contentView.layer.borderWidth = 1.0 //I also tried with 0.0
cell.contentView.layer.borderColor = UIColor.clear.cgColor
cell.contentView.layer.masksToBounds = true

但它似乎不起作用。

我尝试将这些行放在下一个函数中:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

甚至在单元格 class 中的函数 awakeFromNib 中。

知道如何删除边框吗?

您尝试的代码是 borderWidth 和边框颜色。

使用下面的线条作为阴影并删除上面的代码。

cell.contentView.layer.masksToBounds = false
cell.contentView.layer.shadowColor = UIColor.black.cgColor
cell.contentView.layer.shadowOpacity = 0.5
cell.contentView.layer.shadowOffset = CGSize(width: -1, height: 1)
cell.contentView.layer.shadowRadius = 1

cell.contentView.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
cell.contentView.layer.shouldRasterize = true

cell.contentView.layer.rasterizationScale = UIScreen.main.scale