I shared how to draw a gradient layer before. Recently, there is a need to make a gradient navigation bar. If you draw a gradient layer to the navigation bar, it cannot cover the status bar, so you still need to use it [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"xxx"] forBarMetrics:UIBarMetricsDefault]
on the backgroundImageView of the navigation bar. Method. Share a method of rendering a gradient image
- (UIImage*)imageWithColor:(NSArray *)colors withSize:(CGSize)size{
UIGraphicsBeginImageContextWithOptions(size, YES, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
NSMutableArray *ar = [NSMutableArray array];
for (UIColor *c in colors) {
[ar addObject:(id)c.CGColor];
}
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(size.width, 0), kCGGradientDrawsBeforeStartLocation|kCGGradientDrawsAfterEndLocation);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
return theImage;
}