Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

black background when overriding drawRect in UIScrollView

So I am trying to override drawRect in my UIScrolLView, however it gives me this black background instead of the background color that I've specified for my UIScrollView. Why is this? If I remove the drawRect code then everything is fine:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if (shouldDrawVerticalLineForProfile){

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGColorRef separatorColor = [UIColor colorWithRed:47.0/255.0 green:47.0/255.0 
                                                     blue:47.0/255.0 alpha:1.0].CGColor;

        // Add at bottom
        CGPoint startPoint = CGPointMake(60, 0);
        CGPoint endPoint = CGPointMake(60, 10000);

        CGContextSaveGState(context);
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextSetStrokeColorWithColor(context, separatorColor);
        CGContextSetLineWidth(context, 5.0);
        CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
        CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
        CGContextStrokePath(context);
        CGContextRestoreGState(context);  

    }

}
like image 962
xonegirlz Avatar asked Jul 03 '12 20:07

xonegirlz


4 Answers

I guess what you are searching for is:

myScrollViewInstance.opaque = NO

After that the background of your scroll view should not be black anymore.

like image 58
PakitoV Avatar answered Nov 20 '22 05:11

PakitoV


I ran into a similar situation when overriding drawRect in my UIScrollView subclass.

Simply overriding with:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}

resulted in a black background instead of the desired clear background I'd get without overriding.

I found it was because the View Background was set to Default in Interface Builder and that setting it to Clear Color resolved the issue for me.

I'm not sure if this is related to your problem but thought I'd share in case it might help.

like image 43
Denton Avatar answered Nov 20 '22 05:11

Denton


The answer here is very clearly documented in the AppleDocs for UIView's drawRect: method. The first sentence is:

The default implementation of this method does nothing.

So, the suggestions here to call super are not going to help. The rest of the answer is contained further down in the discussion:

... if the opaque property of your view is set to YES, your drawRect: method must totally fill the specified rectangle with opaque content.

Meaning that if you don't intend to have a transparent background, you need to actually draw the background. The most correct way to do this is to use the following drawRect: template:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect]; // optional if a direct UIView-subclass, should be called otherwise.

    CGContextRef context = UIGraphicsGetCurrentContext();
    // Fill the background color, if needed
    if (self.opaque) {
        UIGraphicsPushContext(context);
        CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
        CGContextFillRect(context, rect);
        UIGraphicsPopContext();
    }

    // Your code here...
}
like image 8
Adam Kaplan Avatar answered Nov 20 '22 04:11

Adam Kaplan


This should help

CGContextSetFillColorWithColor(context, colorBack);
CGContextFillRect(context, self.bounds); 

// Choose bounds and colorBack accordingly

like image 4
S.P. Avatar answered Nov 20 '22 06:11

S.P.