设置导航栏的透明度,先要设置纯色背景图,再隐藏导航栏下方的黑线,这样看起来才会跟内容更加连贯!
//1设置一个纯色image,给uiColor添加类别方法:
+(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;
}
//2 设置背景及透明度
UIImage *navImage = [UIColor createImageWithColor: [UIColor colorWithRed:130.0/255 green:0.0/255 blue:255.0/255 alpha:0.5]];
[self.navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
//3 隐藏黑线
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];