ios 带有圆角和阴影的 UICollectionViewCell 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18113872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:38:47  来源:igfitidea点击:

UICollectionViewCell with rounded corners AND drop shadow not working

iosobjective-ccalayeruicollectionviewcell

提问by jj0b

I want my UICollectionViewCells to have rounded corners and drop shadows but I have run into a problem where it seems I can only have one or the other, but not both.

我希望我的 UICollectionViewCells 具有圆角和阴影,但我遇到了一个问题,似乎我只能拥有一个,但不能同时拥有。

To just round the corners I use this code in the initialization of the cell:

为了绕过拐角,我在单元格的初始化中使用了以下代码:

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

To just add a drop shadow I use this code in the initialization of the cell:

为了添加阴影,我在单元格的初始化中使用了以下代码:

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]];

To try and have rounded corners and a drop shadow I use this code in the initialization of the cell:

为了尝试圆角和阴影,我在单元格的初始化中使用了以下代码:

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]];

but this results in the drop shadow only.

但这只会导致阴影。

Is this a bug or am I doing something wrong?

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

回答by Gennadiy Ryabkin

Works for me great:

对我很有用:

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

回答by Lal Krishna

If you place all your subviews into the UICollectionViewCellcontent view, which you probably are, you can set the shadow on the cell's layer and the border on the contentView's layer to achieve both results.

如果您将所有子视图放入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

斯威夫特 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

回答by Dennis

I think I ran into a similar issue. My problem was that clipping in my subviews of the UICollectionViewCelldidn't work properly with shadows and rounded borders. The exact same code worked just fine before when I had that view (as a standard UIViewsubclass though) in a UIScrollView.

我想我遇到了类似的问题。我的问题是在我的子视图中的剪辑UICollectionViewCell无法正常使用阴影和圆角边框。完全相同的代码在我UIViewUIScrollView.

So long story short, I moved all this setup from the initWithCoderto a later place after getting it from -dequeueReusableCellWithReuseIdentifier:forIndexPath:. Solved the problem for me. Seems like UICollectionViewsare doing something I wouldn't expect to their cells' layers at some point?

长话短说,我将所有这些设置initWithCoder-dequeueReusableCellWithReuseIdentifier:forIndexPath:. 为我解决了问题。似乎UICollectionViews在某个时候对他们的细胞层做了一些我不期望的事情?

回答by Vov4yk

There is tricky moment. Cutting corners and dropping shadow is mutually exclusive function in one layer. Dropping shadow is frame extension process, but corners is the process of masking to bounds.

有棘手的时刻。切角和阴影是一层互斥的功能。投下阴影是框架扩展的过程,而角落是遮罩到边界的过程。

Solution is in function separation. I recommend setup shadow for the cell layer, but cut corners for contentView layer of that cell.

解决方法是功能分离。我建议为单元格层设置阴影,但为该单元格的 contentView 层切角。

回答by Yung Dai

If you are using a subclass to make the collection just make sure you do the following.

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

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];

works like a charm.

奇迹般有效。