本文介绍了UICollectionViewCell带圆角和投影不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的UICollectionViewCells有圆角和阴影,但我遇到了一个问题,似乎我只能有一个或另一个,但不是两个。
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.
为了绕过角落我在单元格的初始化中使用此代码:
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]];
但这只会产生投影。
这是一个错误还是我做错了什么?
Is this a bug or am I doing something wrong?
推荐答案
为我工作很棒:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.layer.masksToBounds = YES;
cell.layer.cornerRadius = 6;
...
return cell;
}
这篇关于UICollectionViewCell带圆角和投影不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!