问题
In my UITableViewController
's cells, I want to display a circle with a number in it. I am using UIBezierPath
's bezierPathWithOvalInRect:
to draw the circle.
Unfortunately, while I can set the fill
color to be clearColor
, the unused portion of the CGRect
passed to bezierPathWithOvalInRect:
is black.
How do I get rid of the black area created?
Partial screenshot for reference:

(I eventually hope to get that number inside the circle)
Code:
LTTTableViewCell:
- (void)awakeFromNib {
[super awakeFromNib];
// Create a square view using the height of the cell
CGRect positionFrame = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.height);
LTTDrawBallView *drawBallView = [[LTTDrawBallView alloc] initWithFrame:positionFrame];
[self.contentView addSubview:drawBallView];
}
LTTDrawBallView:
- (void)drawRect:(CGRect)rect
{
// Create a new rect with some padding
// + create a circle from this new rect:
CGRect box = CGRectInset(self.bounds, self.bounds.size.width * 0.1f, self.bounds.size.height * 0.1f);
UIBezierPath *ballBezierPath = [UIBezierPath bezierPathWithOvalInRect:box];
[[UIColor whiteColor] setStroke];
[[UIColor greenColor] setFill]; // Green here to show the black area
[ballBezierPath stroke];
[ballBezierPath fill];
[self setBackgroundColor:[UIColor clearColor]]; // Happens with and without this line
}
回答1:
In the init method of your LTTDrawBallView, include the code:
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
来源:https://stackoverflow.com/questions/18115639/ios-draw-clear-circle-with-uibezierpath-bezierpathwithovalinrect