UICollectionView Cell Shadow

Avi*_*rma 7 objective-c calayer ios uicollectionview uicollectionviewcell

我正在尝试向我的自定义添加阴影UICollectionViewCell,这是我在自定义集合视图单元类中使用的代码:

self.layer.shadowOffset = CGSizeMake(1, 0);
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = .25;
Run Code Online (Sandbox Code Playgroud)

这为集合视图单元的组件提供了阴影.

Vin*_*ose 15

不要忘记添加这两行

self.clipsToBounds = false      
self.layer.masksToBounds = false
Run Code Online (Sandbox Code Playgroud)


Ani*_*pta 8

Swift 4.2 和 xcode 10

这是添加阴影的代码。

    cell.contentView.layer.cornerRadius = 2.0
    cell.contentView.layer.borderWidth = 1.0
    cell.contentView.layer.borderColor = UIColor.clear.cgColor
    cell.contentView.layer.masksToBounds = true

    cell.layer.backgroundColor = UIColor.white.cgColor
    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 2.0)//CGSizeMake(0, 2.0);
    cell.layer.shadowRadius = 2.0
    cell.layer.shadowOpacity = 1.0
    cell.layer.masksToBounds = false
    cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).cgPath
Run Code Online (Sandbox Code Playgroud)