iphone - 渐变背景 UIScrollView

标签 iphone objective-c ios uiviewcontroller uiscrollview

如何给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); 
}

不知道直接在UIScrollViewdrawRect中绘制渐变会有什么效果。

关于iphone - 渐变背景 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9713249/

相关文章:

objective-c - 将字符串转换为 NSDate

ios - 选择器 'datePicker' 没有已知的类方法

ios - 旋转 UIImage

ios - 以编程方式 Swift 设置初始 View Controller

iphone - 从 IBAction 方法调用时听不到 AudioServicesPlaySystemSound 声音

iphone - MKAnnotationView 自定义图像未显示

iphone - 在 iPhone 上拖动 UIScrollView 时,OpenGL ES View 中的动画卡住

ios - 我想使用 Storyboard在同一 View 中添加 waitview

ios - 如何动态生成 TableView 部分并在该特定部分中对相似值进行分组?

iphone - Mapkit addAnnotation异常: unrecognized selector sent to instance