效果:动态给UINavigationBar添加属性,通过设置属性的值来实现导航条的透明雨渐变。
UINavigationBar+ZJFadeShow.h的具体实现
@interface UINavigationBar (ZJFadeShow)
/**
* 动态给UINavigationBar添加属性,通过设置属性的值来实现导航条的透明雨渐变。
*/
- (void)zj_setBackgroundColor:(UIColor *)backgroundColor;
- (void)zj_setElementsAlpha:(CGFloat)alpha;
- (void)zj_setTranslationY:(CGFloat)translationY;
- (void)zj_reset;
@end
UINavigationBar+ZJFadeShow.m的具体实现
#import "UINavigationBar+ZJFadeShow.h"
#import
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
@implementation UINavigationBar (ZJFadeShow)
static char overlayKey;
- (UIView *)overlay
{
return objc_getAssociatedObject(self, &overlayKey);
}
- (void)setOverlay:(UIView *)overlay
{
objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)zj_setBackgroundColor:(UIColor *)backgroundColor
{
if (!self.overlay) {
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + 20)];
self.overlay.userInteractionEnabled = NO;
self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth; // Should not set `UIViewAutoresizingFlexibleHeight`
[[self.subviews firstObject] insertSubview:self.overlay atIndex:0];
}
self.overlay.backgroundColor = backgroundColor;
}
- (void)zj_setTranslationY:(CGFloat)translationY
{
self.transform = CGAffineTransformMakeTranslation(0, translationY);
}
- (void)zj_setElementsAlpha:(CGFloat)alpha
{
[[self valueForKey:@"_leftViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
view.alpha = alpha;
}];
[[self valueForKey:@"_rightViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
view.alpha = alpha;
}];
UIView *titleView = [self valueForKey:@"_titleView"];
titleView.alpha = alpha;
// when viewController first load, the titleView maybe nil
[[self subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
obj.alpha = alpha;
}
if ([obj isKindOfClass:NSClassFromString(@"_UINavigationBarBackIndicatorView")]) {
obj.alpha = alpha;
}
}];
}
- (void)zj_reset
{
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.overlay removeFromSuperview];
self.overlay = nil;
}
@end
具体使用
设置导航栏的背景为透明
[self.navigationController.navigationBar zj_setBackgroundColor:[UIColor clearColor]];
滑动过程中改变导航栏的颜色
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[_headView scrollViewDidScroll:scrollView];
UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY > NAVBAR_CHANGE_POINT) {
CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
[self.navigationController.navigationBar zj_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
} else {
[self.navigationController.navigationBar zj_setBackgroundColor:[color colorWithAlphaComponent:0]];
}
}
注:NAVBAR_CHANGE_POINT为自定义宏,向上滑动超过这个高度导航栏的颜色才开始改变。
移出设置:
[self.navigationController.navigationBar zj_reset];