如何给UIScrollView添加渐变背景?我有一个带有 UIScrollView 的 View Controller 。我想为 ScrollView 添加渐变背景。我也有一个背景渐变图像,但它不完全适合 ScrollView 的内容大小。
请帮忙。 谢谢
最佳答案
我之前已经将 UIScrollView
backgroundColor
设置为清除,然后在 ScrollView 后面放置一个渐变 View 。然后在 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);
}
不知道直接在UIScrollView
的drawRect
中绘制渐变会有什么效果。
关于iphone - 渐变背景 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9713249/