給導(dǎo)航條設(shè)置背景圖片就會取消它的半透明效果
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setBackgroundImage:[self image:[UIImage imageNamed:@"純白"] withColor:[XNWGlobalColor navigationColor]]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[navigationBar setShadowImage:[UIImage new]];
}
給Image著色,我這里的導(dǎo)航條顏色是不固定的音榜,所以需要根據(jù)不同顏色生成跟導(dǎo)航條相同的顏色庞瘸,以此來隱藏分割線
//改變圖片顏色
- (UIImage *)image:(UIImage *)image withColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextFillRect(context, rect);
UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
以上設(shè)置背景圖片需要注意,如果使用的圖片過大就會使得所有非繼承UIScrollView的控制器向下偏移一定的位置赠叼,建議使用1px的圖片進(jìn)行平鋪(沒有驗(yàn)證是否可用)
還有一種隱藏導(dǎo)航條與View的分割線(實(shí)際就是一個圖片)擦囊,這種方式不會使別的控制器有偏移,并且可以保持導(dǎo)航條的半透明效果嘴办,看大家的需求了
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
NSArray *list=self.navigationController.navigationBar.subviews;
for (id obj in list) {
if ([obj isKindOfClass:[UIImageView class]]) {
UIImageView *imageView=(UIImageView *)obj;
NSArray *list2=imageView.subviews;
for (id obj2 in list2) {
if ([obj2 isKindOfClass:[UIImageView class]]) {
UIImageView *imageView2=(UIImageView *)obj2;
imageView2.hidden=YES;
}
}
}
}
}
希望對大家有用K渤 !