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 }
新聞熱點
疑難解答