开发者

Custom background drawing in an grouped UITableViewCell

I'm having some trouble drawing a custom gradient background in a UITableViewCell when the style is set to 'grouped' and the cell is first or the last one of the section. My approach is to simply create a CAGradientLayer and add it to the view like this:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = rect;
gradient.colors = [NSArray arrayWithObjects:(id)[_backgroundColorLight CGColor], (id)[_backgroundColorDark CGColor], nil];
[self.backgroundView.layer insertSublayer:gradient atIndex:0];
self.backgroundView.layer.masksToBounds = YES;

Unfortunately this produces cells like this one:开发者_运维知识库

Custom background drawing in an grouped UITableViewCell

Does anyone have a hint on how to make the background fit the boarders of the cell?

Thanks

–f


If you draw a custom background, you also have to draw the borders yourself. There's quite some open source stuff out there.

Basically, you need to know [in the cell] if it's top/middle/bottom/single, and cut the stuff in drawRect. You won't come far with insertSublayer.

Check out the PageCellBackground class you find here: http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html


I've not tried it but CAGradientLayer happens to be a subclass of CALayer, so perhaps setting its borderRadius may work. But it rounds all corners then, so you may compensate for that by making the layer bigger than the cell and have the cell view cut it off.

Look at Rounded UIView using CALayers - only some corners - How? for more examples

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜