I have some code that I use to fill a view with a linear gradient, I’ve refactored this into a helper method. This draws fine on iOS 5.x but when I tried running on 4.3.5 I don’t get anything. I’ve checked all the CG calls and they all are supported on 4.0 or older, it look so simplistic I can’t see why it doesn’t work on 4.x.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef startColor = [UIColor lightGrayColor].CGColor;
CGColorRef endColor = [UIColor blackColor].CGColor;
CGRect paperRect = self.frame;
[GraphicsHelpers drawLinearGradientWithContext:context
inRect:paperRect
startColor:startColor
endColor:endColor];
}
Here is that method
+ (void)drawLinearGradientWithContext:(CGContextRef)context inRect:(CGRect)rect startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
I found most of this code here and was working great until I started testing on older OS versions as I’m close to release.
If you’re using supported API and you’re getting different results from one version of the OS to another, file a bug report.