3

I'm trying to draw a straight dashed line. I've gotten it so it draws the dashes, but this black background color remains. I've seem many answers to other questions, but none of them worked.

Apple's documentation seems to point to the fill color and using either CGContextFillPath or CGContextDrawPath, but neither of those work and the background of the dashed line is still black.

- (void)drawRect:(CGRect)rect {
    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    GLFloat lines[] = {self.frame.size.width, self.frame.size.width*2};

    CGFloat grey[4] = {0.6f, 0.6f, 0.6f, 1.0f};
    CGContextSetStrokeColor(contextRef, grey);
    CGContextSetFillColorWithColor(contextRef, [[UIColor clearColor] CGColor]);

    CGContextSetLineDash(contextRef, 0, lines, 2);
    CGContextSetLineWidth(contextRef, self.frame.size.width);

    CGContextBeginPath(contextRef);
    CGContextMoveToPoint(contextRef, 0, 0);
    CGContextAddLineToPoint(contextRef, 0, self.frame.size.height);

    CGContextDrawPath(contextRef, kCGPathFillStroke);
}
DrDisc
  • 281
  • 4
  • 12
  • BTW, `lines` probably should be `CGFloat` just to match the expected type of `CGContextSetLineDash`. – Rob Aug 08 '13 at 15:59
  • Oops, capitalization error, not the actual cause of my problem though :) – DrDisc Aug 08 '13 at 18:03
  • Quite right, not your issue here. But I think you want `CGFloat`, not `GLfloat` (the former, I believe, can be `double` on Mac OS, whereas the latter looks like it's always `float`). Not an issue on iOS, tho. More of a matter of style than substance. – Rob Aug 08 '13 at 18:22

1 Answers1

1

When manually adding this view to your view hierarchy, you just have to make sure you're setting the background color. For example, if you're initializing your view with initWithFrame, then set the background color in that method:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

Or, alternatively, when adding the view, set the background color there:

CustomView *customView = [[CustomView alloc] initWithFrame:frame];
customView.backgroundColor = [UIColor clearColor];
[self.view addSubview:customView];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    I'd just add a small precision to clear his misunderstanding of CGContextFillPath. Filling a path is litterally filling the inside of the path, thus the path should be closed (Like a square, or a circle). You can't fill a line going from top to bottom. And it certainly wouldn't help fill the whole view if the path doesn't enclose the whole view. – Nerkatel Aug 08 '13 at 15:58
  • @Nerkatel Agreed. I'd probably just use `kCGPathStroke` when drawing a line like this, not `kCGPathFillStroke`. Also, the use of the `width` when setting the dashing of a vertical line is curious. And because the line is so wide, half of it is falling outside of the view. It's a curious implementation, but I assume he has his reasons. – Rob Aug 08 '13 at 16:02
  • I had tried setting background color in drawRect before, but I guess I was just doing it out of context? The use of width is because this line will never be more than 2 pixels wide. – DrDisc Aug 08 '13 at 16:10
  • Thank you all for the prompt replies :) – DrDisc Aug 08 '13 at 16:12
  • 1
    @DrDisc No problem. In answer to your comment, doing it in `drawRect` is too late. You want to set the background color during the configuration of the view. – Rob Aug 08 '13 at 16:19