去除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 ...
随机推荐
- 启动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, ...
- 关于cocos2d-x精灵加亮及变灰效果
//根据现有CCSprite,变亮和变灰 static CCSprite* graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { //CC ...
- Performance Tuning of Spring/Hibernate Applications---reference
http://java.dzone.com/articles/performance-tuning For most typical Spring/Hibernate enterprise appli ...
- android开发之单点触摸
相对于多点触摸,单点触摸还是很简单的. 新建一个工程,先看看布局文件: <RelativeLayout xmlns:android="http://schemas.android.co ...
- Java使用jackson问题解决
Java使用jackson问题解决 >>>>>>>>>>>>>>>>>>>>&g ...
- Codeforces Round #310 (Div. 2)--A(简单题)
http://codeforces.com/problemset/problem/556/A 题意:给一个01字符串,把所有相邻的0和1去掉,问还剩下几个0和1. 题解:统计所有的0有多少个,1有多少 ...
- sharepoint中的YesNo字段
sharepoint中的YesNo字段实际上是一个Boolean字段,性格有点特别,如果IsShow是一个YesNo字段,使用caml查询的时候值为”1“(Yes)”0“(No),Item[IsSho ...
- C# 反射之属性操作
一.反射-类操作 //1.获取对象所有的属性名 Student stu = new Student(); //获取当前类名称 Console.WriteLine(stu.GetType().Name) ...
- 使用本地光盘安装Microsoft .NET Framework 3.5 for Win8/WinServer2012
如何使用本地源安装Microsoft .NET Framework 3.5 作为SQL Server 2012的 必要组件,校验组件过程有个小BUG,即使没有安装也通过,但会卡在安装环节(enabli ...
- 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 ...