gpt4 book ai didi

iphone - 向 UICollectionViewCell 添加圆角和阴影

转载 作者:IT王子 更新时间:2023-10-29 07:30:57 29 4
gpt4 key购买 nike

所以我已经浏览了各种关于添加第二个 View 以添加阴影的帖子,但是如果我想在 UICollectionViewCell 中添加它,我仍然无法让它工作。我对 UICollectionViewCell 进行了子类化,这是我的代码,其中我将各种 UI 元素添加到单元格的内容 View 并向图层添加阴影:

[self.contentView setBackgroundColor:[UIColor whiteColor]];

self.layer.masksToBounds = NO;
self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1.0;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
[self.layer setShadowPath:[[UIBezierPath bezierPathWithRect:self.bounds] CGPath]];

我想知道如何给 UICollectionViewCell 添加圆角和阴影。

最佳答案

这些解决方案都不适合我。如果您将所有 subview 放入 UICollectionViewCell 内容 View (您可能就是这样),您可以在单元格层上设置阴影并在 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 blackColor].CGColor;
cell.layer.shadowOffset = CGSizeMake(0, 2.0f);
cell.layer.shadowRadius = 2.0f;
cell.layer.shadowOpacity = 0.5f;
cell.layer.masksToBounds = NO;
cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;

swift 3.0

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

self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 2.0)
self.layer.shadowRadius = 2.0
self.layer.shadowOpacity = 0.5
self.layer.masksToBounds = false
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.contentView.layer.cornerRadius).cgPath

关于iphone - 向 UICollectionViewCell 添加圆角和阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505379/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com