1

我在 UIView 类别中使用以下内容为 UITableView 添加了一个阴影(它覆盖了屏幕底部的三分之一 - 请参阅随附的屏幕截图):

- (void) addShadow {
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 1;
    self.layer.shadowOffset = CGSizeMake(-5,-5);
    self.layer.shadowRadius = 20;
    self.layer.shadowPath = path.CGPath;
    self.layer.shouldRasterize = YES;
}

它按预期显示,但是当我向上滚动时,阴影也会向上滚动。此外,表格滚动超出其上限。你能建议这里有什么问题吗?如果我评论self.layer.masksToBounds = NO;,阴影消失,但表格滚动是预期的。因此,问题可能出在某个地方masksToBounds

初始状态

滚动状态

4

1 回答 1

2

我通过在下面放置一个相同的视图来解决它,只是为了阴影。不是一个干净的解决方案......因此我仍然愿意接受答案。我的代码如下:

- (UIView*) addShadow {
    UIView* backView = [[UIView alloc] initWithFrame:self.frame];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:backView.bounds];
    backView.layer.masksToBounds = NO;
    backView.layer.shadowColor = [UIColor blackColor].CGColor;
    backView.layer.shadowOpacity = 1;
    backView.layer.shadowOffset = CGSizeMake(-5,-5);
    backView.layer.shadowRadius = 20;
    backView.layer.shadowPath = path.CGPath;
    backView.layer.shouldRasterize = YES;
    [self.superview addSubview:backView];
    [self.superview bringSubviewToFront:self];
    return backView;
}

  • (void) removeShadow { self.layer.masksToBounds = YES; self.layer.shadowColor = nil; self.layer.shadowOpacity = 0; self.layer.shadowOffset = CGSizeMake(0,0); self.layer.shadowRadius = 0; }
于 2013-03-11T00:43:47.287 回答