简体   繁体   English

如何为具有清晰背景颜色的 UICollectionViewCell 添加阴影?

[英]How to add shadow to UICollectionViewCell with clear background color?

I am populating the collection view cells.我正在填充集合视图单元格。 But only for a single cell, I want a different background color with shadow to the cell.但仅对于单个单元格,我想要一个不同的背景颜色和单元格的阴影。 So far I am able to give background color but can't find any idea, how to give shadow.到目前为止,我能够提供背景颜色,但找不到任何想法,如何提供阴影。 Any suggestions...有什么建议...

cell.backgroundColor = UIColor.init(alpha: 1, red: 255, green: 221, blue: 126)

let view = UIView()
view.frame = CGRect(x: 20, y: cell.frame.origin.y + 62, width: cell.frame.size.width, height: 50)
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 3, height: 3)
view.layer.shadowOpacity = 0.9
view.backgroundColor = UIColor.clear
self.addSubview(view)

return cell

This is what I did这就是我所做的

这就是我所做的

This is what I want这就是我要的

这就是我要的

I can give you a way of doing it.我可以给你一个方法。

Give your cell a clear color给你的细胞一个清晰的颜色

Add a child view in it make sure to give proper constrains (make sure to keep the child view inside the cell).在其中添加子视图确保提供适当的约束(确保将子视图保留在单元格内)。

Add all labels or any other views inside the new child view在新的子视图中添加所有标签或任何其他视图

Give shadows and background color to the child view.为子视图提供阴影和背景颜色。 (make sure to toggle the color when needed just as selected color) (确保在需要时切换颜色与所选颜色一样)

I am sure this will definitely look the image you have sown我相信这肯定会看起来像你播下的图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM