去除UINavigationBar默认透明度的方法
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默认透明度的方法的更多相关文章
- $Android去除系统默认的标题栏和全屏的三种方法
在做应用的时候,很多时候是不需要系统自带的标题栏的,而是自己去实现标题栏,这就要去掉系统的标题栏,下面总结了三种方法.全屏也是一样的道理,也总结了实现的三种方法. (一)去除标题栏 1.方法1 在Ac ...
- dedecms中去除首页index.html的方法
本文介绍了dedecms中去除首页index.html的方法,有需要的朋友参考下. dedecms织梦cms建站程序输入地址后,而打开的实际地址后面有个index.html. 这里分享下两种解决方 ...
- Win7去除桌面残影的方法
用户升级到Win7系统后使用正常,就是系统桌面会留有残影,怎么样也去不掉,影响用户的使用,那么要如何将这些残影去掉呢?可从计算机属性中进行相关配置. 解决方法 一.在计算机面板上,右键点击“计算机”, ...
- MySQL设置当前时间为默认值的方法
方法一.是用alert table语句: 复制代码代码如下: use test_db1; create table test_ta1( id mediumint(8) unsigned not nul ...
- 修改mysql默认字符集的方法
+--------------------------+---------------------------------+ | Variable_name | Value | +---------- ...
- 最基本的session保存法,类似于默认的files方法
关于session的几个补充函数 在PHP下,关于session的讨论很多,其实在PHP4中还有几个函数是我们平时没有注意到的. 下面我把它们介绍给大家吧. 其中的session_set_save_h ...
- java提供的默认list排序方法-转
1.java提供的默认list排序方法 主要代码: List<String> list = new ArrayList();list.add("刘媛媛"); list. ...
- Exporter - 实现默认的导入方法用于模块
Exporter - 实现默认的导入方法用于模块 简介: In module YourModule.pm: package YourModule; require Exporter; @ISA = q ...
- 切换Ubuntu系统python默认版本的方法
另附切换系统python默认版本的方法: 先使用命令: update-alternatives --list python 查看python命令的各种可能结果, 例如我的结果: /usr/bin/py ...
随机推荐
- SpringNote02.Blog系统迁移到Linux下
基于SpringMVC-Hibernate的博客系统还在继续开发中 . 项目地址:https://github.com/defshine/SpringBlog 整个项目迁移到linux下开发,安装in ...
- trace 日志
关闭 ORACLE trace 日志功能 alter system set trace_enabled=false select * from v$parameter where NAME lik ...
- trace openjdk from systemtap
here are several different tactics to trace openjdk from systemtap. The first relies on sys/sdt.h dt ...
- Android 自定义View修炼-自定义View-带百分比进度的圆形进度条(采用自定义属性)
很多的时候,系统自带的View满足不了我们功能的需求,那么我们就需要自己来自定义一个能满足我们需求的View,自定义View我们需要先继承View,添加类的构造方法,重写父类View的一些方法,例如o ...
- Android 自定义View修炼-Android开发之自定义View开发及实例详解
在开发Android应用的过程中,难免需要自定义View,其实自定义View不难,只要了解原理,实现起来就没有那么难. 其主要原理就是继承View,重写构造方法.onDraw,(onMeasure)等 ...
- Android 高级UI设计笔记01:使用ExpandableListView组件(ListView的扩展)
1.ExpandableListView是一个用来显示二级节点的ListView. 比如如下效果的界面: 2.使用ExpandableListView步骤 (1)要给ExpandableListVie ...
- Linux网络相关命令小结
# ifconfig # ifup/ifdown # route -n # ip link show //显示本机所有接口信息 # traceroute # netstat //查看本机网络连接与后门 ...
- 为当前的div 动态添加一个样式
$("#target").addClass("newClass");
- The hacker's sanbox游戏
第一关:使用/usr/hashcat程序,对passwd中root的密码进行解密,得到gravity98 执行su,输入密码gravity98. 第二关:获取提供的工具,wget http://are ...
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\d29b5393\123c3a1c\App_Code.odl3w4o6.dll”--“拒绝访问。 ”
IIS部署网站或者Webservice时,出现以下问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...