Draw shadow on top of UITableViewCell

2019-03-17 06:15发布

How can I draw a shadow on the top of a UITableViewCell? Tweetbot does this, here's a screenshot:

enter image description here

1条回答
干净又极端
2楼-- · 2019-03-17 06:43

You could use a CAGradientLayer. In your cellForRowAtIndexPath, when creating a cell, create a gradient layer and add it to your cell. Note, you'll have to import the QuartzCore framework. Like so,

        //top shadow
        UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 10)];
        CAGradientLayer *topShadow = [CAGradientLayer layer];
        topShadow.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 10);
        topShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.25f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
        [topShadowView.layer insertSublayer:topShadow atIndex:0];

        [cell.contentView addSubview:topShadowView];
查看更多
登录 后发表回答