0

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?

CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • I think this could help you out: http://stackoverflow.com/questions/813068/uitableview-change-section-header-color – Rui Peres Jul 18 '11 at 12:06

2 Answers2

1

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.

Ian1971
  • 3,666
  • 7
  • 33
  • 61
  • Not sure why CGRectGetMidY(currentBounds) is used on the bottomCenter. I think what you want is actually the bottom Y and not the mid Y. I changed this to the total height and it worked much better for me. Other than that this is great though. – clarky Apr 14 '12 at 18:02
0

you can use this

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

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

    }
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
  • that didn't work. it removed the nice-look of the tableView headers, and also removed the title – CodeGuy Jul 18 '11 at 13:23