之前也写过一篇关于导航渐变的博客,链接地址如下:
http://blog.csdn.net/minggeqingchun/article/details/52571810
但是这篇博客所写的文章和目前这篇原理差不多,都是在滑动时,根据滑动的Y值改变去改变导航颜色的alpha值,但是测试多次发现还是偶尔会浮现一些bug出来,因此优化了一下直接给UINavigationBar增加分类
- (void)setBackgroundColor:(UIColor *)backgroundColor{
if (!self.overlay) {
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 20)];
self.overlay.userInteractionEnabled = NO;
self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self insertSubview:self.overlay atIndex:0];
//去掉一横线
if ([self respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
NSArray *list = self.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;
}
}
}
}
}
}
self.overlay.backgroundColor = backgroundColor;
}