uiview subclass showing up as black rectangles first time setneedsdisplay is called, but ok after that

StackOverflow https://stackoverflow.com/questions/15867727

Question

I have a custom UIView subclass which overrides drawrect. The view gets its data from a datasource, and passes some of that information to its delegate, the view controller, to process it into the strings that it needs to draw. The first time that setneedsdisplay is called, the view shows up as a black rectangle, but it works just fine if it is called a second time. All of the frame sizes and positions get set properly so I am sure the delegate and datasource is being set properly from the onset. Here is the offending drawrect code:

- (void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0, 0, 0, 1.0};
    CGColorRef color = CGColorCreate(colorSpace, components);
    ;
    //CGContextStrokeRect(context, CGRectMake(1, 1, self.frame.size.width-1.0, self.frame.size.height-1.0));

    CGContextMoveToPoint(context, 0, [_delegate denominatorPoint:self].y);
        if ([_delegate numeratorBiggerThanDenominator:self]==YES) 
    CGContextAddLineToPoint(context,[[_delegate numeratorString:self]sizeWithFont:[_delegate fontToDrawWith:self]].width , [_delegate denominatorPoint:self].y);
        else
            CGContextAddLineToPoint(context,[[_delegate denominatorString:self]sizeWithFont:[_delegate fontToDrawWith:self]].width , [_delegate denominatorPoint:self].y);
    if ([_delegate hasAFraction:self]==YES) {


    CGContextSetStrokeColorWithColor(context, color);


    CGContextStrokePath(context);

    }

    CGColorSpaceRelease(colorSpace);
    CGColorRelease(color);



    self.backgroundColor=[UIColor clearColor];


    [[_delegate numeratorString:self] drawAtPoint:[_delegate numeratorPoint:self] withFont:[_delegate fontToDrawWith:self]];
    NSLog([_delegate numeratorString:self]);
    if ([_delegate hasAFraction:self]==YES) 
        [[_delegate denominatorString:self] drawAtPoint:[_delegate denominatorPoint:self] withFont:[_delegate fontToDrawWith:self]];
    [[_delegate Variable:self] drawAtPoint:[_delegate variablePoint:self] withFont:[_delegate fontToDrawWith:self]];
    if ([_delegate hasAParenthesTerm:self]==YES) {
        //NSLog(@"parentheses being drawn");
        NSString* openParentheses=@" (";
        [openParentheses drawAtPoint:[_delegate openParenthesesPoint:self] withFont:[_delegate fontToDrawWith:self]];
        NSString* closedParentheses=@")";
        [closedParentheses drawAtPoint:[_delegate closeParenthesesPoint:self] withFont:[_delegate fontToDrawWith:self]];
    }

}

Let me know if there is other code that would help shine light on the problem.

Was it helpful?

Solution

Did you try moving self.backgroundColor = [UIColor clearColor]; to the init method?

You should only need to run that line once anyway, and I could imagine that calling it in the middle of the draw method might lead to an issue (and in the second call to drawRect, the backgroundColor would already be set to the clearColor, so UIView could ignore it, making your issue go away with the second setNeedsDisplay call)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top