在我的iPhone应用程序中,我想更改UITableView上的标题。除了更改颜色之外,如何保持灰色,默认标头的渐变/ alpha /漂亮外观?
最佳答案
如果要应用渐变,则可以创建一个自定义视图类,然后重写drawRect:并使用CoreGraphics绘制渐变
例如
- (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);
}
然后按照Sachin的建议进行操作,但改用您的自定义视图类。