Search code examples
iosswiftuitableviewborderxib

remove border in UICollectionView when cells loaded from xib


I have a collectionView and my cells are loaded from a xib.

I'm trying to remove the border (I want only shadow) with the following lines:

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

But it doesn't seems to be working.

I try put these lines in the next function:

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

And even in the function awakeFromNib in the cell class.

Any idea how can I remove the border ?


Solution

  • The code you are trying is for borderWidth and border color.

    Use below lines for shadow and remove the above code.

    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