UICollectionViewCell带圆角和投影不起作用

我希望我的UICollectionViewCells有圆角和阴影,但我遇到了一个问题,似乎我只能有一个或另一个,但不是两个。

为了绕过角落我在单元的初始化中使用此代码:

CALayer *layer = [self layer]; [layer setCornerRadius:4]; [layer setRasterizationScale:[[UIScreen mainScreen] scale]]; [layer setShouldRasterize:YES]; 

要添加一个投影,我在单元格的初始化中使用此代码:

 CALayer *layer = [self layer]; [layer setMasksToBounds:NO]; [layer setRasterizationScale:[[UIScreen mainScreen] scale]]; [layer setShouldRasterize:YES]; [layer setShadowColor:[[UIColor blackColor] CGColor]]; [layer setShadowOffset:CGSizeMake(0.0f,0.5f)]; [layer setShadowRadius:8.0f]; [layer setShadowOpacity:0.2f]; [layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]]; 

要尝试使用圆角和投影,我在单元格的初始化中使用此代码:

 CALayer *layer = [self layer]; [layer setMasksToBounds:NO]; [layer setCornerRadius:4]; [layer setRasterizationScale:[[UIScreen mainScreen] scale]]; [layer setShouldRasterize:YES]; [layer setShadowColor:[[UIColor blackColor] CGColor]]; [layer setShadowOffset:CGSizeMake(0.0f,0.5f)]; [layer setShadowRadius:8.0f]; [layer setShadowOpacity:0.2f]; [layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]]; 

但这只会产生阴影。

这是一个错误还是我做错了什么?

对我很有用:

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { ... cell.layer.masksToBounds = YES; cell.layer.cornerRadius = 6; ... return cell; } 

如果将所有子视图放入UICollectionViewCell内容视图(可能是您),则可以在单元格图层上设置阴影,在contentView图层上设置边框以实现两种结果。

 cell.contentView.layer.cornerRadius = 2.0f; cell.contentView.layer.borderWidth = 1.0f; cell.contentView.layer.borderColor = [UIColor clearColor].CGColor; cell.contentView.layer.masksToBounds = YES; cell.layer.shadowColor = [UIColor lightGrayColor].CGColor; cell.layer.shadowOffset = CGSizeMake(0, 2.0f); cell.layer.shadowRadius = 2.0f; cell.layer.shadowOpacity = 1.0f; cell.layer.masksToBounds = NO; cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath; 

Swift 4.0

 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.shadowColor = UIColor.lightGray.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 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 

我想我遇到了类似的问题。 我的问题是我的UICollectionViewCell子视图中的剪辑无法正常使用阴影和圆角边框。 在我在UIScrollView中拥有该视图(作为标准UIView子类)之前,完全相同的代码工作得很好。

长话短说,我从-dequeueReusableCellWithReuseIdentifier:forIndexPath:获取后,将所有这些设置从initWithCoder移到了以后的地方。 解决了我的问题。 看起来像UICollectionViews正在做一些我不希望他们的细胞层在某些时候?

有一个棘手的时刻。 切角和阴影是一层互斥的function。 丢弃阴影是帧扩展过程,但角落是屏蔽边界的过程。

解决方案是function分离。 我建议为单元格图层设置阴影,但为该单元格的contentView图层剪切角。

如果您正在使用子类来进行集合,请确保执行以下操作。

 CALayer *layer = [self layer]; [layer setCornerRadius:_cornerRadius]; [layer setRasterizationScale:[[UIScreen mainScreen] scale]]; [layer setShouldRasterize:YES]; [layer setShadowColor:[[UIColor blackColor] CGColor]]; [layer setShadowOffset:CGSizeMake(0.0,4.0)]; [layer setShadowRadius:6.0f]; [layer setShadowOpacity:0.25]; [layer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:layer.cornerRadius] CGPath]]; self.contentView.layer.cornerRadius = _cornerRadius; self.contentView.layer.borderWidth= _borderWidth; self.contentView.layer.borderColor = _borderColor.CGColor; self.contentView.backgroundColor = [UIColor whiteColor]; self.backgroundColor = [UIColor clearColor]; 

奇迹般有效。