I want to create a custom UIView
that has a transparent gradient background in order to make text more visible over the view behind this custom view.
Here's an example image:
Picture the orange box is a UIView
under my transparent gradient view. I'd then put text on top of the transparent gradient view and because of how it's darker, text would be more visible, but you can still see some of the orange box from behind it.
I'm having trouble figuring out how to accomplish this with UIView
.
In my custom UIView
's drawRect
I have:
- (void)drawRect:(CGRect)rect
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
NSArray *gradientColors = @[(id)[UIColor colorWithWhite:0.0 alpha:0.0].CGColor,
(id)[UIColor colorWithWhite:0.0 alpha:0.55].CGColor,
(id)[UIColor colorWithWhite:0.0 alpha:0.7]];
CGFloat gradientLocations[] = {0, 0.77, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) gradientColors, gradientLocations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGGradientRelease(gradient);
}
But it doesn't seem to support transparency, as when I add that UIView
to self.view
it's completely opaque black.
This is how I would do it in CSS:
background-image: linear-gradient(-180deg, rgba(0,0,0,0.00) 0%, rgba(0,0,0,0.55) 23%, rgba(0,0,0,0.70) 100%);
But again, I have no idea how to do it for a custom UIView
subclass.