iOS navigationBar和tabBar变透明 & navigationBar根据滑动距离的渐变色实现

时间:2016-08-30 16:00:50   收藏:0   阅读:483

    navigationBar变为纯透明

    //第一种方法

    //导航栏纯透明

    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

    //去掉导航栏底部的黑线

    self.navigationBar.shadowImage = [UIImage new];

    

    //第二种方法

    [[self.navigationBar subviews] objectAtIndex:0].alpha = 0;

 

 

    tabBar同理

    [self.tabBar setBackgroundImage:[UIImage new]];

    self.tabBar.shadowImage = [UIImage new];

 

    navigationBar根据滑动距离的渐变色实现

    //第一种

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        CGFloat offsetToShow = 200.0;//滑动多少就完全显示

        CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;

        [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha;

    }

    //第二种

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        CGFloat offsetToShow = 200.0;

        CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;

        

        [self.navigationController.navigationBar setShadowImage:[UIImage new]];

        [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];

    }

    

    //生成一张纯色的图片

    - (UIImage *)imageWithColor:(UIColor *)color

    {

        CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

        UIGraphicsBeginImageContext(rect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [color CGColor]);

        CGContextFillRect(context, rect);

        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        

        return theImage;

    }

原文:http://www.cnblogs.com/tongyuling/p/5822342.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!