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

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

[self.navigationController.navigationBar setTranslucent:YES];

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

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

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

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

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

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

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

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

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic) BOOL flag;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad]; [self.view setBackgroundColor:[UIColor lightGrayColor]]; // 默认带有一定透明效果,可以使用以下方法去除系统效果
[self.navigationController.navigationBar setTranslucent:NO]; _flag=YES; // 默认navigationBar是隐藏的
[self.navigationController setNavigationBarHidden:_flag animated:YES]; UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
scrollview.backgroundColor=[UIColor purpleColor];
[self.view addSubview:scrollview]; // 给scrollView添加点击事件
UITapGestureRecognizer *gest=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clicked)];
[scrollview addGestureRecognizer:gest];
} //
// scrollView添加点击事件
//
-(void)clicked
{
_flag=!_flag;
[self.navigationController setNavigationBarHidden:_flag animated:YES]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

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

隐藏时:

显示时:

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

动态显示时

看一下对比图:

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

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

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

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

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

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

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

自定义UINavigationBar,继承系统的UINavigationBar

MyNavigationBar.h文件

#import <UIKit/UIKit.h>

@interface MyNavigationBar : UINavigationBar

@property(nonatomic, strong) CALayer *extraColorLayer;

@end

MyNavigationBar.m文件

#import "MyNavigationBar.h"

@implementation MyNavigationBar

-(void)setBarTintColor:(UIColor *)barTintColor
{
[super setBarTintColor:barTintColor];
if (self.extraColorLayer == nil) {
self.extraColorLayer = [CALayer layer];
self.extraColorLayer.opacity = ;// 不透明度
[self.layer addSublayer:self.extraColorLayer];
}
self.extraColorLayer.backgroundColor = barTintColor.CGColor;
}
-(void)layoutSubviews
{
[super layoutSubviews];
if (self.extraColorLayer != nil) {
[self.extraColorLayer removeFromSuperlayer];
self.extraColorLayer.opacity = ;
[self.layer insertSublayer:self.extraColorLayer atIndex:];
CGFloat spaceAboveBar = self.frame.origin.y;
self.extraColorLayer.frame = CGRectMake(, - spaceAboveBar, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) + spaceAboveBar);
}
} @end

其中self.extraColorLayer.opacity设置的是不透明度,我这边这设置为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];
以上代码设置导航颜色

不知道说明白没,先总结到这吧

参考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. 用nginx图片缓存服务器

    图片的存储硬件 把图片存储到什么介质上? 如果有足够的资金购买专用的图片服务器硬件或者 NAS 设备,那么简单的很: 如果上述条件不具备,只想在普通的硬盘上存储,首先还是要考虑一下物理硬盘的实际处理能 ...

  2. 64位开源处理器Rocket该人士介绍

    最近大概读一点UCB发布时间Rocket处理器的源代码,的每个文件的源代码的功能有一定的一般理解,Mark一点点. Rocket是一家64bit标量处理器,5第一阶段管道,用途risc-v指令集.综合 ...

  3. HDU2149-Good Luck in CET-4 Everybody!(博弈,打表找规律)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. android UI生成器

    可根据选择的效果生成资源 http://jgilfelt.github.io/android-actionbarstylegenerator/#name=example&compat=sher ...

  5. AS3性能及Flex-Formatting设置问题

    1.支持Vector 2.for each in更是从Flash Player 9 3.Flash原生的bitmap.encode 4.如打开位图缓存:使用bitmapData.lock:把bitma ...

  6. PHP 内存的分布问题

    php运行,内存分为5个区域, 1.基本数据类型--->栈区 2.符合数据类型-->堆区 对象实例在堆区,对象名字在栈区(指向此对象实例的变量)

  7. 关于Androdi中SQLITE 3采用GBK编码存储,数据库中文乱码问题。

    1.最近开发一个项目,用SQLite Expert Personal打开数据库如下图,title会产生乱码,问题. 2.由于SQL lite默认是存储UTF-8格式,后来更改数据库编码类型为ANSI, ...

  8. Java线程:新特征-线程池

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...

  9. QVW中实现日期区间的选择功能!

    QV在日期的选择上不是很灵活,日期区段的选择可以在列表框中直接用鼠标拖拉区段,如果跨周期比较长了还是不是很方便啦. 下面介绍的方式是完全实现了起始日期的选择功能. 注:日期这个字段在抽取的时候一定要格 ...

  10. 【转】怎样创建一个Xcode插件(Part 1)

      原文:How To Create an Xcode Plugin: Part 1/3 原作者:Derek Selander 译者:@yohunl 译者注:原文使用的是xcode6.3.2,我翻译的 ...