//先声明
CAGradientLayer *_headerLayer;
//确定要渐变的颜色,切记颜色顺序和透明度要一一对应
UIColor *colorOne = [UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:0.7];
UIColor *colorTwo = [UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:0.0];
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];
NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];
if (!_headerLayer) {
//crate gradient layer
_headerLayer = [CAGradientLayer layer];
_headerLayer.colors = colors;
_headerLayer.locations = locations;
}
_headerLayer.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20.0);
[self.viewController.view.layer addSublayer:_headerLayer];