一、设置系统导航栏颜色
/**
* 给targetController的导航条设置颜色和透明度
*
* @param color
* @param alpha
* @param targetController
*/
- (void)setNavigationBarColor:(UIColor *)color
alpha:(CGFloat)alpha
targetController:(UIViewController *)targetController
{
targetController.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
UIImage *image = [self createAImageWithColor:color alpha:alpha];
[targetController.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
/**
* 根据颜色和透明度生成一张图片
*
* @param color
* @param alpha
*
* @return
*/
- (UIImage *)createAImageWithColor:(UIColor *)color
alpha:(CGFloat)alpha
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextSetAlpha(context, alpha);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
二、导航栏透明度渐变效果
5月-17-2016 10-57-27.gif
网友评论