美文网首页iOS小日常
2018最新的设置导航栏透明和渐变色方法,只要一行代码

2018最新的设置导航栏透明和渐变色方法,只要一行代码

作者: 七叔huangsf | 来源:发表于2018-05-09 22:31 被阅读28次

    2018最新的设置导航栏透明和渐变色方法,只要一行代码

    在你的baseVC中添加如下代码:


    #pragma mark 导航栏颜色

    //设置导航栏透明

    //导航栏渐变色

    -(void)setNavigationBarColor:(UIColor *)color alpha:(double)alpha{

        [self.navigationController.navigationBar setBarTintColor:[color colorWithAlphaComponent:alpha]];//主题色

        //这里包含了一个大的背景imageView 和一个底线imageView

        NSArray *barBackgroundSubViews = [[[self.navigationController.navigationBar subviews] objectAtIndex:0] subviews];

        for (UIView *subview in barBackgroundSubViews) {

            subview.alpha = alpha;

    //        subview.backgroundColor = color;

        }

    }


    OK,大功告成!

    接下来,在需要的页面里添加下面的代码ok了

    #pragma mark -life cycle

    -(void)viewWillAppear:(BOOL)animated{

        [super viewWillAppear:animated];

        self.navigationController.navigationBar.translucent = YES;

        //导航栏透明

        [self setNavigationBarColor:[UIColor clearColor] alpha:0.00];

        //修改导航栏的颜色

        [self scrollViewDidScroll:self.collectionView];

    }

    -(void)viewWillDisappear:(BOOL)animated{

        [super viewWillDisappear:animated];

        self.navigationController.navigationBar.translucent = NO;

        //导航栏主题色

        [self setNavigationBarColor:k_themeColor alpha:1.00];

    }

    相关文章

      网友评论

        本文标题:2018最新的设置导航栏透明和渐变色方法,只要一行代码

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