您的位置:首页 > 其它

NavigationController 设置导航栏主题

2016-07-12 15:45 477 查看
/**
*  系统在第一次使用这个类的时候调用(1个类只会调用一次)
*/
+ (void)initialize
{
// 设置导航栏主题
UINavigationBar *navBar = [UINavigationBar appearance];
// 设置背景图片
NSString *bgName = nil;
if (iOS7) { // 至少是iOS 7.0
bgName = @"NavBar64";
} else { // 非iOS7
bgName = @"NavBar";
}
[navBar setBackgroundImage:[UIImage imageNamed:bgName] forBarMetrics:UIBarMetricsDefault];

// 设置标题文字颜色
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
[navBar setTitleTextAttributes:attrs];

//设置BarButtonItem的主题
UIBarButtonItem *item=[UIBarButtonItem appearance];
//设置文字颜色
NSMutableDictionary *itemAttrs=[NSMutableDictionary dictionary];
itemAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:14];
itemAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
[item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
if (!iOS7) {
//设置按钮背景

[item setBackgroundImage:[UIImage imageNamed:@"NavButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[item setBackgroundImage:[UIImage imageNamed:@"NavButtonPressed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

//设置返回按钮的背景
[item setBackButtonBackgroundImage:[UIImage imageNamed:@"NavButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[item setBackButtonBackgroundImage:[UIImage imageNamed:@"NavButtonPressed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

}
else{
navBar.tintColor=[UIColor whiteColor];
}
}


<div><strong>A.导航栏两侧文字按钮</strong></div><div>1.需求:</div><div>所有导航栏两侧的文字式按钮统一样式</div><div>普通样式:橙色</div><div>高亮样式:红色</div><div>不可用样式:亮灰</div><div>阴影:不使用</div><div><a target=_blank href="http://font.knowsky.com/">字体</a>大小:15</div><div> </div><div>github: <a target=_blank href="https://github.com/hellovoidworld/HVWWeibo">https://github.com/hellovoidworld/HVWWeibo</a></div><div> </div><div>2.实现效果</div><div>默认样式:<div><strong>A.导航栏两侧文字按钮</strong></div><div>1.需求:</div><div>所有导航栏两侧的文字式按钮统一样式</div><div>普通样式:橙色</div><div>高亮样式:红色</div><div>不可用样式:亮灰</div><div>阴影:不使用</div><div><a target=_blank href="http://font.knowsky.com/">字体</a>大小:15</div><div> </div><div>github: <a target=_blank href="https://github.com/hellovoidworld/HVWWeibo">https://github.com/hellovoidworld/HVWWeibo</a></div><div> </div><div>2.实现效果</div><div>默认样式:defaultNavTitlteItemStyle</div><div> </div><div>统一使用样式:modifiedNavBarButtonTemStyle</div>
</div><div>3.思路

在创建item的时候逐个设置:代码超级冗余
抽取创建公共父类:稍好的选择,但是继承了此公共父类的控制器,就不能操作其去继承系统自带的控制器类了,造成很大的隐患。iOS中控制器不建议提取公共父类,最好直接继承系统自带控制器。
</div>使用主题appearance统一设置所有UIBarButtonItem的样式:采用!在自定义的UINavigationController的类初始化方法中实现一次,就可以改变所有使用了此类的BarButtonItem样式

4.实现
HVWNavigationViewController.m:

1 /** 类初始化的时候调用 */
2 + (void)initialize {
3     // 初始化导航栏样式
4     [self initNavigationBarTheme];
5
6     // 初始化导航栏item样式
7     [self initBarButtonItemTheme];
8 }
9
10 /** 统一设置导航栏item的样式
11 * 因为是通过主题appearence统一修改所有NavivationBar的样式,可以使用类方法
12 */
13 + (void) initBarButtonItemTheme {
14     // 设置导航栏,修改所有UINavigationBar的样式
15     UIBarButtonItem *appearance = [UIBarButtonItem appearance];
16
17     // 设置noraml状态下的样式
18     NSMutableDictionary *normalTextAttr = [NSMutableDictionary dictionary];
19     // 字体大小
20     normalTextAttr[NSFontAttributeName] = [UIFont systemFontOfSize:15];
21     // 字体颜色
22     normalTextAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
23     // 设置为normal样式
24     [appearance setTitleTextAttributes:normalTextAttr forState:UIControlStateNormal];
25
26     // 设置highlighted状态下的样式
27     NSMutableDictionary *highlightedTextAttr = [NSMutableDictionary dictionaryWithDictionary:normalTextAttr];
28     // 字体颜色
29     highlightedTextAttr[NSForegroundColorAttributeName] = [UIColor redColor];
30     // 设置为normal样式
31     [appearance setTitleTextAttributes:highlightedTextAttr forState:UIControlStateHighlighted];
32
33     // 设置disabled状态下的样式
34     NSMutableDictionary *disabledTextAttr = [NSMutableDictionary dictionaryWithDictionary:normalTextAttr];
35     // 字体颜色
36     disabledTextAttr[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
37     // 设置为normal样式
38     [appearance setTitleTextAttributes:disabledTextAttr forState:UIControlStateDisabled];
39
40 }

B.设置导航栏样式
1.需求:

统一显示文字颜色:黑色
文字阴影:禁止
字体大小:20

Image(73)

2.思路:同“A”

3.实现:
同“A"
HVWNavigationViewController.m:

1 /** 统一设置导航栏样式 */
2 + (void) initNavigationBarTheme {
3     // 使用appearence(主题)设置,统一修改所有导航栏样式
4     UINavigationBar *appearance = [UINavigationBar appearance];
5
6     // 为了统一iOS6和iOS7,iOS6需要设置导航栏背景来模拟iOS7的效果
7     if (!iOS7) {
8         [appearance setBackgroundImage:[UIImage imageWithNamed:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
9     }
10
11     // 设置属性
12     NSMutableDictionary *attr = [NSMutableDictionary dictionary];
13     // 设置字体
14     attr[NSForegroundColorAttributeName] = [UIColor blackColor];
15     attr[NSFontAttributeName] = [UIFont systemFontOfSize:20];
16     // 消去文字阴影,设置阴影偏移为0
17     NSShadow *shadow = [[NSShadow alloc] init];
18     shadow.shadowOffset = CGSizeZero;
19     attr[NSShadowAttributeName] = shadow;
20
21     [appearance setTitleTextAttributes:attr];
22 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: