方法一:
设置的位置是在视情况而定
-(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];