2012-03-15 152 views
2

如何添加渐变背景到UIScrollView?我有一个UIScrollView在其上的viewcontroller。我想为scrollview添加渐变背景。我也有一个用于背景的渐变图像,但它并不完全适合滚动视图的内容大小。渐变背景UIScrollView

请帮忙。 谢谢

回答

8

我以前设置UIScrollViewbackgroundColor清除,然后在滚动视图后面放置一个渐变视图。然后画上UIView像一个梯度:

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

    CGGradientRef glossGradient; 
    CGColorSpaceRef rgbColorspace; 
    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.0, 1.0 }; 
    CGFloat components[8] = { 0.119, 0.164, 0.192, 1.000, // Start color 
     0.286, 0.300, 0.307, 1.000 }; // End color 

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

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

    CGGradientRelease(glossGradient); 
    CGColorSpaceRelease(rgbColorspace); 
} 

我不知道什么样的影响直接在UIScrollViewdrawRect将有绘制渐变。