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

转载自网友“tongyuling”
原博客地址 http://www.cnblogs.com/tongyuling/p/5822342.html

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;

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,821评论 25 709
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,310评论 12 197
  • 大部分的后端会很很鄙视前端。我也不知道为什么,可能大部分人都会觉得脚本语言根本不算语言。 大多人 会叫我们切图仔,...
    小黑的眼阅读 3,465评论 0 15
  • 改良版土豆丝饼 经过上次的理论学习与实践操作,于土豆丝饼这张考卷还算交了一份答案吧,但还是觉...
    yiyi_1650阅读 311评论 0 6
  • 鹧鸪天·秋殇 词/红尘一凡 冷雨飕飕洒阁窗, 黄昏无语对帘霜。 阶前花落声渐远, 帐内芳菲欲断肠。 捎雁信,问斜...
    37度女人_8dda阅读 530评论 3 4