0
votes

I hope I am able to explain my actual problem. I have got a navigation controller whose root controller is a Table view controller. I want to paint the Table view controller background by adding a gradient sub view as a background. Every thing works well but the gradient background is now apply just under the navigation bar, even if this is translucent. In fact I see it as translucent but in this configuration, the background just start below the navigation bar. I want also the navigation bar be gradient like the background below. How can I do it? Is my question clear enough? Thank you very much.

[self drawGradientBackground];
- (void)drawGradientBackground
{
    self.view.backgroundColor = [UIColor clearColor];
    RMABackGround *background = [[RMABackGround alloc] initWithFrame:self.view.bounds];
    background.gradientColors =  @[[UIColor colorWithRed:52/255.0f green:153/255.0f blue:55/255.0f alpha:1.0f], [UIColor colorWithRed:53/255.0f green:168/255.0f blue:224/255.0f alpha:1.0f]];
    [self.view addSubview:background];
}

Into RMABackGround class:


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

    NSMutableArray *colors = [NSMutableArray arrayWithCapacity:[self.gradientColors count]];
    [self.gradientColors enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UIColor class]]) {
            [colors addObject:(__bridge id)[obj CGColor]];
        } else if (CFGetTypeID((__bridge void *)obj) == CGColorGetTypeID()) {
            [colors addObject:obj];
        } else {
            @throw [NSException exceptionWithName:@"CRGradientLabelError"
                                           reason:@"Object in gradientColors array is not a UIColor or CGColorRef"
                                         userInfo:NULL];
        }
    }];

    CGContextSaveGState(context);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0, -rect.size.height);

    CGGradientRef gradient = CGGradientCreateWithColors(NULL, (__bridge CFArrayRef)colors, NULL);

    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint,
                                kCGGradientDrawsAfterEndLocation | kCGGradientDrawsBeforeStartLocation);

    CGGradientRelease(gradient);
    CGContextRestoreGState(context);

    [super drawRect:rect];
}
1
hi riccardo, it'll help if you add a screenshot and the code how you initialize and add the gradient :)nburk
ah, I think you need a certain score to add screenshots to questions. try to obtain that score then. and one more hint: please update the question with the code so that it's actually readable rather than posting it in the comments :)nburk
how can I paste code to better explain my questions?user4461166
Anyone can answer my question?user4461166
click edit below your question and then use markdown to format your codenburk

1 Answers

0
votes

I solved this issue. In fact this was not directly related to gradient background as I thought at beginning. Instead I was wrong with table view and in particular with how to set color for table view background and table view cell background. But now I solved. Issue closed.