CodeGuy
CodeGuy

Reputation: 28905

UITableView headers - iPhone

In my iPhone app, I'd like to alter the headers on my UITableView. How can I keep the same gradient/alpha/nice looking style of the gray, default headers, except change the color of it?

Upvotes: 0

Views: 1064

Answers (2)

Ian1971
Ian1971

Reputation: 3706

If you want to apply a gradient then you could create a custom view class and then override drawRect: and use CoreGraphics to draw a gradient e.g.

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

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1 };

    const CGFloat *startColorComponents = CGColorGetComponents(startColor.CGColor);
    const CGFloat *endColorComponents = CGColorGetComponents(endColor.CGColor);

    CGFloat components[8] = { startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3],  // Start color
        endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3] }; // End color

    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, bottomCenter, 0);

    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace); 
}

Then do as per Sachin's suggestion but use your custom view class instead.

Upvotes: 1

Tendulkar
Tendulkar

Reputation: 5540

you can use this

   - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    headerView.backgroundColor = [UIColor lightGrayColor];
    return headerView;

    }

Upvotes: 0

Related Questions