Fork me on GitHub

UINavigationBar透明

最近项目里面用到了UINavigationBar透明的问题

透明的UINavigationBar

透明效果的实现只要几行很简单的代码

1
2
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];

但是我发现如果该页面是push进来的页面,并且pop回去之后就会出现UINavigationBar依旧为透明不能够变为原来的颜色,所以需要在重新设置其颜色

1
2
[self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = nil;

此方法可以重写UINavigationController的类,然后所有的类都集成该类,代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// CustomNavigationController.m
// SJFood
//
// Created by 叶帆 on 14/11/25.
// Copyright (c) 2014年 Ye Fan. All rights reserved.
//
#import "CustomNavigationController.h"
#import "HomeViewController.h"
#import "UserInfoViewController.h"
#import "IndividualViewController.h"
@interface CustomNavigationController ()
@end
@implementation CustomNavigationController
#pragma mark - UIViewController Methods
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.hidesBottomBarWhenPushed = YES;
[super pushViewController:viewController animated:animated];
}
#pragma mark - UINavigationControllerDelegate methods
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([viewController isKindOfClass:[HomeViewController class]])
{
[self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = nil;
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationBar.barTintColor = kMainProjColor;//导航条的颜色
self.navigationBar.tintColor = [UIColor whiteColor];//左侧返回按钮,文字的颜色
self.navigationBar.barStyle = UIStatusBarStyleLightContent;
}
else if([viewController isKindOfClass:[UserInfoViewController class]] || [viewController isKindOfClass:[IndividualViewController class]])
{
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationBar.tintColor = [UIColor whiteColor];//左侧返回按钮,文字的颜色
self.navigationBar.barStyle = UIStatusBarStyleLightContent;
}
else
{
[self.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = nil;
self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: kMainBlackColor};
self.navigationBar.barTintColor = [UIColor whiteColor];//导航条的颜色
self.navigationBar.tintColor = kMainProjColor;//左侧返回按钮,文字的颜色
self.navigationBar.barStyle = UIStatusBarStyleDefault;
}
}
@end

版权声明



Ivan’s Blog by Ivan Ye is licensed under a Creative Commons BY-NC-ND 4.0 International License.
叶帆创作并维护的叶帆的博客博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证

本文首发于Ivan’s Blog | 叶帆的博客博客( http://yeziahehe.com ),版权所有,侵权必究。

本文链接:http://yeziahehe.com/2015/07/29/UINavigationBar_translate/