设置UINavigationBar透明

方法一:

设置的位置是在视情况而定

-(void)viewWillAppear:(BOOL)animated{

           [super viewWillAppear:animated];

           [self navigationBarAlpha:0];

}

-(void)viewWillDisappear:(BOOL)animated{

         [super viewWillDisappear:animated];

         [self navigationBarAlpha:1];

}

-(void)navigationBarAlpha:(float)alpha

{

// [XCRGBColor(52, 51, 57)设置背景颜色

[self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:[XCRGBColor(52, 51, 57) colorWithAlphaComponent:alpha]]

forBarMetrics:UIBarMetricsDefault];

// 去掉bar下面的分割线

self.navigationController.navigationBar.shadowImage = [UIImage new];

// 设置透明度

self.navigationController.navigationBar.translucent = alpha == 0;

}

-(UIImage*) createImageWithColor:(UIColor*) color

{

CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return theImage;

}

方法二:

self.navigationController.navigationBar.translucent=YES;

[self.navigationController.navigationBarsetBackgroundImage:[UIImagenew]

forBarMetrics:UIBarMetricsDefault];

self.navigationController.navigationBar.shadowImage= [UIImagenew];

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

推荐阅读更多精彩内容