UINavigationbar的属性translucent,用来控制导航条的透明度的;

iOS7+版本后,navigationbar的translucent属性默认为YES,及默认带有透明度

  1. [self.navigationController.navigationBar setTranslucent:YES];

接下来,我们说说为什么要去除透明度:

在做项目过程中,美工给出的效果图,根据给出的颜色值(或用取色工具取到的颜色值)去设置导航的颜色时,

  1. //ios7以下的版本设置导航栏背景颜色可以使用
  2. [[UINavigationBar appearance] setTintColor:[UIColor orangeColor]];
  3. //iOS7以后:
  4. [[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];

发现颜色总是不对,默认translucent=YES,发现颜色一直和效果图不一样,因为带有一定的透明度

所以去除透明度方法一:设置translucent=NO即可

  1. // 默认带有一定透明效果,可以使用以下方法去除系统效果
  2. [self.navigationController.navigationBar setTranslucent:NO];

这样以为万事大吉,就继续项目,有动态隐藏显示navigationBar的需求,那么问题来了,在动态隐藏显示navigationBar时,遇到问题了

先看看例子Demo吧,再说问题是什么

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()<UIGestureRecognizerDelegate>
  4.  
  5. @property (nonatomic) BOOL flag;
  6.  
  7. @end
  8.  
  9. @implementation ViewController
  10.  
  11. - (void)viewDidLoad {
  12. [super viewDidLoad];
  13.  
  14. [self.view setBackgroundColor:[UIColor lightGrayColor]];
  15.  
  16. // 默认带有一定透明效果,可以使用以下方法去除系统效果
  17. [self.navigationController.navigationBar setTranslucent:NO];
  18.  
  19. _flag=YES;
  20.  
  21. // 默认navigationBar是隐藏的
  22. [self.navigationController setNavigationBarHidden:_flag animated:YES];
  23.  
  24. UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
  25. scrollview.backgroundColor=[UIColor purpleColor];
  26. [self.view addSubview:scrollview];
  27.  
  28. // 给scrollView添加点击事件
  29. UITapGestureRecognizer *gest=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clicked)];
  30. [scrollview addGestureRecognizer:gest];
  31. }
  32.  
  33. //
  34. // scrollView添加点击事件
  35. //
  36. -(void)clicked
  37. {
  38. _flag=!_flag;
  39. [self.navigationController setNavigationBarHidden:_flag animated:YES];
  40.  
  41. }
  42.  
  43. - (void)didReceiveMemoryWarning {
  44. [super didReceiveMemoryWarning];
  45. // Dispose of any resources that can be recreated.
  46. }
  47.  
  48. @end

运行以上代码,会发现,navigationBar带动画的显示隐藏时,scrollView也在跟着动,如下图

隐藏时:

显示时:

后来发现,注释掉设置translucent=NO,即[self.navigationController.navigationBar setTranslucent:NO];这句后scrollView就不动了,如下图:

动态显示时

看一下对比图:

scrollView不跟着动的问题解决了,但是navigationBar的颜色又不对了。。。

所以单纯的设置transluncent=NO,无法满足动态显示隐藏navigationBar的需求,所以需求两种兼容的方法

那么去除默认透明度的方法二就诞生了

在UIViewController的viewDidLoad方法中进行如下设置:

  1. UIColor *barColour = [UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
  2. UIView *colourView = [[UIView alloc] initWithFrame:CGRectMake(.f, -.f, self.view.size.width, .f)];
  3. colourView.opaque = NO;
  4. colourView.alpha = .7f;
  5. colourView.backgroundColor = barColour;
  6. self.navigationController.navigationBar.barTintColor = barColour;
  7. [self.navigationController.navigationBar.layer insertSublayer:colourView.layer atIndex:];

为了不在每个viewController中设置(哪怕是写了基类viewController,其他viewController继承它) ,做以下处理

去除默认透明度的方法三:

自定义UINavigationBar,继承系统的UINavigationBar

MyNavigationBar.h文件

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface MyNavigationBar : UINavigationBar
  4.  
  5. @property(nonatomic, strong) CALayer *extraColorLayer;
  6.  
  7. @end

MyNavigationBar.m文件

  1. #import "MyNavigationBar.h"
  2.  
  3. @implementation MyNavigationBar
  4.  
  5. -(void)setBarTintColor:(UIColor *)barTintColor
  6. {
  7. [super setBarTintColor:barTintColor];
  8. if (self.extraColorLayer == nil) {
  9. self.extraColorLayer = [CALayer layer];
  10. self.extraColorLayer.opacity = ;// 不透明度
  11. [self.layer addSublayer:self.extraColorLayer];
  12. }
  13. self.extraColorLayer.backgroundColor = barTintColor.CGColor;
  14. }
  15. -(void)layoutSubviews
  16. {
  17. [super layoutSubviews];
  18. if (self.extraColorLayer != nil) {
  19. [self.extraColorLayer removeFromSuperlayer];
  20. self.extraColorLayer.opacity = ;
  21. [self.layer insertSublayer:self.extraColorLayer atIndex:];
  22. CGFloat spaceAboveBar = self.frame.origin.y;
  23. self.extraColorLayer.frame = CGRectMake(, - spaceAboveBar, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + spaceAboveBar);
  24. }
  25. }
  26.  
  27. @end

其中self.extraColorLayer.opacity设置的是不透明度,我这边这设置为1,让其没有透明度

  1. UINavigationController* rootController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:nil];
    [rootController setViewControllers:@[[[ViewController alloc] init]]];
    [rootController.navigationBar setBarTintColor:[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:]];
    [rootController setNavigationBarHidden:NO];
  1. 以上代码设置导航颜色
  2.  
  3. 不知道说明白没,先总结到这吧
  4.  
  5. 参考http://stackoverflow.com/questions/18897485/achieving-bright-vivid-colors-for-an-ios-7-translucent-uinavigationbar/19534709

去除UINavigationBar默认透明度的方法的更多相关文章

  1. $Android去除系统默认的标题栏和全屏的三种方法

    在做应用的时候,很多时候是不需要系统自带的标题栏的,而是自己去实现标题栏,这就要去掉系统的标题栏,下面总结了三种方法.全屏也是一样的道理,也总结了实现的三种方法. (一)去除标题栏 1.方法1 在Ac ...

  2. dedecms中去除首页index.html的方法

    本文介绍了dedecms中去除首页index.html的方法,有需要的朋友参考下. dedecms织梦cms建站程序输入地址后,而打开的实际地址后面有个index.html.   这里分享下两种解决方 ...

  3. Win7去除桌面残影的方法

    用户升级到Win7系统后使用正常,就是系统桌面会留有残影,怎么样也去不掉,影响用户的使用,那么要如何将这些残影去掉呢?可从计算机属性中进行相关配置. 解决方法 一.在计算机面板上,右键点击“计算机”, ...

  4. MySQL设置当前时间为默认值的方法

    方法一.是用alert table语句: 复制代码代码如下: use test_db1; create table test_ta1( id mediumint(8) unsigned not nul ...

  5. 修改mysql默认字符集的方法

    +--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...

  6. 最基本的session保存法,类似于默认的files方法

    关于session的几个补充函数 在PHP下,关于session的讨论很多,其实在PHP4中还有几个函数是我们平时没有注意到的. 下面我把它们介绍给大家吧. 其中的session_set_save_h ...

  7. java提供的默认list排序方法-转

    1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list. ...

  8. Exporter - 实现默认的导入方法用于模块

    Exporter - 实现默认的导入方法用于模块 简介: In module YourModule.pm: package YourModule; require Exporter; @ISA = q ...

  9. 切换Ubuntu系统python默认版本的方法

    另附切换系统python默认版本的方法: 先使用命令: update-alternatives --list python 查看python命令的各种可能结果, 例如我的结果: /usr/bin/py ...

随机推荐

  1. 启动hadoop,报错Error JAVA_HOME is not set and could not be found

    报如错误:JAVA_HOME is not set and could not be found,可能是因为JAVA_HOME环境没配置正确,还有一种情况是即使各结点都正确地配置了JAVA_HOME, ...

  2. 关于cocos2d-x精灵加亮及变灰效果

    //根据现有CCSprite,变亮和变灰 static CCSprite* graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { //CC ...

  3. Performance Tuning of Spring/Hibernate Applications---reference

    http://java.dzone.com/articles/performance-tuning For most typical Spring/Hibernate enterprise appli ...

  4. android开发之单点触摸

    相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.co ...

  5. Java使用jackson问题解决

    Java使用jackson问题解决 >>>>>>>>>>>>>>>>>>>>&g ...

  6. Codeforces Round #310 (Div. 2)--A(简单题)

    http://codeforces.com/problemset/problem/556/A 题意:给一个01字符串,把所有相邻的0和1去掉,问还剩下几个0和1. 题解:统计所有的0有多少个,1有多少 ...

  7. sharepoint中的YesNo字段

    sharepoint中的YesNo字段实际上是一个Boolean字段,性格有点特别,如果IsShow是一个YesNo字段,使用caml查询的时候值为”1“(Yes)”0“(No),Item[IsSho ...

  8. C# 反射之属性操作

    一.反射-类操作 //1.获取对象所有的属性名 Student stu = new Student(); //获取当前类名称 Console.WriteLine(stu.GetType().Name) ...

  9. 使用本地光盘安装Microsoft .NET Framework 3.5 for Win8/WinServer2012

    如何使用本地源安装Microsoft .NET Framework 3.5 作为SQL Server 2012的 必要组件,校验组件过程有个小BUG,即使没有安装也通过,但会卡在安装环节(enabli ...

  10. CentOS PHP-5.4.8 编译安装之初体验

    1. 下载5.4.8 版本 [root@Test data] wget http://museum.php.net/php5/php-5.4.8.tar.gz 2. 解压 [root@Test php ...