去除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(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];
以上代码设置导航颜色

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

参考http://stackoverflow.com/questions/18897485/achieving-bright-vivid-colors-for-an-ios-7-translucent-uinavigationbar/19534709

转载于:https://www.cnblogs.com/china-fanny/p/4789489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值