去除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 ...
随机推荐
- 数据库:JDBC编程
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于运行SQL语句的Java API.能够为多种关系数据库提供统一訪问.它由一组用Java语言编写的类和接口 ...
- NSLayoutConstraint-代码实现自己主动布局的函数使用方法说明
[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelat ...
- hdu 4773 Problem of Apollonius
莫名其妙就AC了-- 圆的反演-- 神马是反演? 快去恶补奥数-- #include<iostream> #include<map> #include<string> ...
- 程序猿的编程神器 - vim
一.官方文档: 当你首次安装好 Vim 之后.能够用 :help tutor 或者 :help tutor@cn 命令.进入一个30分钟的 Vim 新手教程.你也能够下载一个 Vim Document ...
- oracle10 权限角色
管理权限和角色 介绍 这一部分我们主要看看oracle中如何管理权限和角色,权限和角色的区别在那里. 当刚刚建立用户时,用户没有任何权限,也不能执行任何操作,oracle数据库会自动创建一个方案, ...
- iOS创建界面方法的讨论
以前在入门的时候,找的入门书籍上编写的 demo 都是基于 Storyboards 拖界面的.后来接触公司项目,发现界面都是用纯代码去写复杂的 autoLayout 的.再然后,领导给我发了个 Mas ...
- WTL的核心机制
WTL背景介绍 WTL是微软ATL开发组成员Nenad Stefanovic先生在ATL Windowing机制上发展起来的一整套GUI框架,运用template技术组织和创建GUI对象,构筑了精致的 ...
- ubuntu 14.04/15.10 安装基于eclipse的android app开发环境
一开始是装了ubuntu15.10,不知道是我的x200机器太old还是iso镜像有问题,总是各种莫名的引导不起来.有时候刚刚装好的干净系统,只install了一个vim和openssh,重启,然后就 ...
- apache安全之修改或隐藏版本信息
修改apache版本信息 在安装之前,编辑原文件httpd-2.2.31/include/ap_release.h文件如下: 40 #define AP_SERVER_BASEVENDO ...
- 自己做的demo---c语言的基本语法,过阵子可以重新写一些算法跟数据结构了
//输入a,b,输出a+b /*#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); ...