UICollectionViewCell带圆角和投影不起作用

jj0*_*j0b 17 objective-c calayer ios uicollectionviewcell

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

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

CALayer *layer = [self layer];
[layer setCornerRadius:4];
[layer setRasterizationScale:[[UIScreen mainScreen] scale]];
[layer setShouldRasterize:YES];
Run Code Online (Sandbox Code Playgroud)

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

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]];
Run Code Online (Sandbox Code Playgroud)

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

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]];
Run Code Online (Sandbox Code Playgroud)

但这只会产生阴影.

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

Gen*_*kin 31

对我很有用:

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

  • maskToBounds将裁剪出阴影效果.下面的Lal Krishna作为一个更好的答案 (2认同)

Lal*_*hna 20

如果将所有子视图放入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;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

  • 应该在layoutSubviews内设置:cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds,cornerRadius: cell.contentView.layer.cornerRadius).cgPath (2认同)

Den*_*nis 2

我想我遇到了类似的问题。我的问题是,在我的子视图中进行剪辑UICollectionViewCell不能正确处理阴影和圆形边框。当我UIViewUIScrollView.

长话短说,initWithCoder在从 获取后,我将所有这些设置从 移到了较晚的位置-dequeueReusableCellWithReuseIdentifier:forIndexPath:。为我解决了问题。似乎UICollectionViews在某些时候他们的细胞层正在做一些我意想不到的事情?