Reputation: 17553
I'm using a slightly modified version of code I grabbed from the top answer here: Modern technique of adding gradient to UIView
My modified code:
- (void)drawRect:(CGRect)rect
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
// The if statements are surely a crappy way of setting the locations, but whatever
CGGradientRef gradient;
if ([self.colors count] == 3) {
CGFloat gradientLocations[] = {0, 0.5, 1};
gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, gradientLocations);
} else {
CGFloat gradientLocations[] = {0, 1};
gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, gradientLocations);
}
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
}
I have put that into a class called Gradient_View
, and I am attempting to subclass it. In my subclass's init
method, I have this:
self.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f].CGColor,
[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.0f].CGColor,
nil];
Note that the second color in that final array has an alpha value of 0, so the gradient should become see-through. However, the result I get in my simulator is totally opaque:
How can I get the transparency to work?
Upvotes: 0
Views: 672
Reputation: 5121
Check and see if your UIView is opaque or not. Make sure opaque is set to NO.
Upvotes: 4