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 }
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 // 消去文字阴影,设置阴影偏移为017 NSShadow *shadow = [[NSShadow alloc] init];18 shadow.shadowOffset = CGSizeZero;19 attr[NSShadowAttributeName] = shadow;20 21 [appearance setTitleTextAttributes:attr];22 }
新闻热点
疑难解答