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(0, 64, self.view.frame.size.width, 200)];
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:6/255.0 green:122/255.0 blue:181/255.0 alpha:1];
UIView *colourView = [[UIView alloc] initWithFrame:CGRectMake(0.f, -20.f, self.view.size.width, 64.f)];
colourView.opaque = NO;
colourView.alpha = .7f;
colourView.backgroundColor = barColour;
self.navigationController.navigationBar.barTintColor = barColour;
[self.navigationController.navigationBar.layer insertSublayer:colourView.layer atIndex:1];
为了不在每个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 = 1;// 不透明度
[self.layer addSublayer:self.extraColorLayer];
}
self.extraColorLayer.backgroundColor = barTintColor.CGColor;
}
-(void)layoutSubviews
{
[super layoutSubviews];
if (self.extraColorLayer != nil) {
[self.extraColorLayer removeFromSuperlayer];
self.extraColorLayer.opacity = 1;
[self.layer insertSublayer:self.extraColorLayer atIndex:1];
CGFloat spaceAboveBar = self.frame.origin.y;
self.extraColorLayer.frame = CGRectMake(0, 0 - 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:6/255.0 green:122/255.0 blue:181/255.0 alpha:1]];
[rootController setNavigationBarHidden:NO];
以上代码设置导航颜色
不知道说明白没,先总结到这吧