So in my custom view, I'm trying to draw a white/gray gradient using Core Graphics. I have the following code:
UIColor *color1 = [UIColor whiteColor];
UIColor *color2 = [UIColor colorWithRed:209.0/255.0 green:212.0/255.0 blue:217.0/255.0 alpha:1.0];
CFMutableArrayRef colors = CFArrayCreateMutable(NULL, 0, NULL);
CFArrayAppendValue(colors, color1.CGColor);
CFArrayAppendValue(colors, color2.CGColor);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {0.0, 1.0};
CGGradientRef gradient = CGGradientCreateWithColors(colorspace, colors, locations);
CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, topCenter, bottomCenter, kCGGradientDrawsAfterEndLocation);
I think this code is fairly straightforward and should result in a nice white/gray gradient. But it doesn't; it draws a transparent/gray gradient.
I think it may have something to do with the view's background color, which is [UIColor clearColor]
. But I cannot change that, as I need to have some portions of my view to be transparent.
Any ideas?
UIColor *color1 = [UIColor whiteColor];
The above line likely creates a color in the greyscale colorspace.
Try this instead:
UIColor *color1 = [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:1.f];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With