导航栏修改透明度

来源:互联网 发布:淘宝手机一键复制宝贝 编辑:程序博客网 时间:2024/05/05 15:02

修改导航透明度最简单的方法就导航设置 self.navigationController.navigationBar.translucent = YES同时在添加 一个透明图片,

如果需求总是更改的话,要总换图片,作为一个懒惰的程序员,不如直接用代码创建个可以改变色值的图片,这样更方便,苹果的程序员就是这么写得哦 

//opacity 修改透明度, CGContextSetRGBFillColor(context, 1142107, opacity); 用来修改色值

- (void)applyTransparentBackgroundToTheNavigationBar:(CGFloat)opacity

{

    UIImage *transparentBackground;

    

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, self.navigationController.navigationBar.layer.contentsScale);

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    CGContextSetRGBFillColor(context, 1, 142, 107, opacity);

    

    UIRectFill(CGRectMake(0, 0, 1, 1));

    

    transparentBackground = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    id navigationBarAppearance = self.navigationController.navigationBar;

    

    [navigationBarAppearance setBackgroundImage:transparentBackground forBarMetrics:UIBarMetricsDefault];

}


0 0
原创粉丝点击