美文网首页OC 开发分类(Category)iOS DeveloperiOS学习笔记
UINavigationBar分类(ZJFadeShow实现导航

UINavigationBar分类(ZJFadeShow实现导航

作者: 逍遥晨旭 | 来源:发表于2017-06-12 14:46 被阅读105次

    效果:动态给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 <objc/runtime.h>
    
    #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];

    相关文章

      网友评论

      • 王健勇:哥,我前端是速成,会看点,但不太会写。最近我在学习APP,总是遇到不少问题,想系统一点学习,请问我应该学习什么软件怎么学习比较好呢,因为没有好友指导,想请教一下前辈
        逍遥晨旭:@王健勇 网上资料很多的,自制力强的好就可以自己学啊!不过我建议你把前端学好,学精了,不缺好工作的,不要追求广而杂,要专而精才好。门门通不如一门精。加油……

      本文标题:UINavigationBar分类(ZJFadeShow实现导航

      本文链接:https://www.haomeiwen.com/subject/xzxtqxtx.html