设置系统导航栏半透明效果

一、设置系统导航栏颜色

/**
 *  给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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容