UINavigationController,NavigationBar,BarButtonItem 相关
1 #import <UIKit/UIKit.h> 2 3 @interface UIBarButtonItem (Extension) 4 + (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action; 5 @end
1 #import "UIBarButtonItem+Extension.h" 2 #import "UIView+Extension.h" 3 4 @implementation UIBarButtonItem (Extension) 5 + (UIBarButtonItem *)itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action 6 { 7 UIButton *button = [[UIButton alloc] init]; 8 [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 9 [button setBackgroundImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted]; 10 11 // 设置按钮的尺寸为背景图片的尺寸 12 button.size = button.currentBackgroundImage.size; 13 14 // 监听按钮点击 15 [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; 16 return [[UIBarButtonItem alloc] initWithCustomView:button]; 17 } 18 @end
1 #import <UIKit/UIKit.h> 2 3 @interface KKNavController : UINavigationController 4 5 @end
1 #import "KKNavController.h" 2 #import "UIBarButtonItem+Extension.m" 3 4 @interface KKNavController () 5 6 @end 7 8 @implementation KKNavController 9 10 /** 11 * 当第一次使用这个类的时候调用1次 12 */ 13 + (void)initialize 14 { 15 // 设置UINavigationBarTheme的主 16 [self setupNavigationBarTheme]; 17 18 // 设置UIBarButtonItem的主题 19 [self setupBarButtonItemTheme]; 20 } 21 22 /** 23 * 设置UINavigationBarTheme的主题 24 */ 25 + (void)setupNavigationBarTheme 26 { 27 UINavigationBar *appearance = [UINavigationBar appearance]; 28 29 // 设置导航栏背景 30 // if (!iOS7) { 31 // [appearance setBackgroundImage:[UIImage imageWithName:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault]; 32 // } 33 34 // 设置文字属性 35 NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary]; 36 textAttrs[UITextAttributeTextColor] = [UIColor blackColor]; 37 // UITextAttributeFont --> NSFontAttributeName(iOS7) 38 #warning 过期 : 并不代表不能用, 仅仅是有最新的方案可以取代它 39 textAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:25]; 40 // UIOffsetZero是结构体, 只要包装成NSValue对象, 才能放进字典\数组中 41 textAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetZero]; 42 [appearance setTitleTextAttributes:textAttrs]; 43 } 44 45 /** 46 * 设置UIBarButtonItem的主题 47 */ 48 + (void)setupBarButtonItemTheme 49 { 50 // 通过appearance对象能修改整个项目中所有UIBarButtonItem的样式 51 UIBarButtonItem *appearance = [UIBarButtonItem appearance]; 52 53 /**设置文字属性**/ 54 // 设置普通状态的文字属性 55 NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary]; 56 textAttrs[UITextAttributeTextColor] = [UIColor orangeColor]; 57 textAttrs[UITextAttributeFont] = [UIFont systemFontOfSize:15]; 58 textAttrs[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetZero]; 59 [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; 60 61 // 设置高亮状态的文字属性 62 NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs]; 63 highTextAttrs[UITextAttributeTextColor] = [UIColor redColor]; 64 [appearance setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted]; 65 66 // 设置不可用状态(disable)的文字属性 67 NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs]; 68 disableTextAttrs[UITextAttributeTextColor] = [UIColor lightGrayColor]; 69 [appearance setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled]; 70 71 /**设置背景**/ 72 // 技巧: 为了让某个按钮的背景消失, 可以设置一张完全透明的背景图片 73 // [appearance setBackgroundImage:[UIImage imageWithName:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 74 } 75 76 /** 77 * 能拦截所有push进来的子控制器 78 */ 79 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated 80 { 81 if (self.viewControllers.count > 0) { // 如果现在push的不是栈底控制器(最先push进来的那个控制器) 82 viewController.hidesBottomBarWhenPushed = YES; 83 84 // 设置导航栏按钮 85 viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_back" highImageName:@"navigationbar_back_highlighted" target:self action:@selector(back)]; 86 viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImageName:@"navigationbar_more" highImageName:@"navigationbar_more_highlighted" target:self action:@selector(more)]; 87 } 88 [super pushViewController:viewController animated:animated]; 89 } 90 91 - (void)back 92 { 93 #warning 这里用的是self, 因为self就是当前正在使用的导航控制器 94 [self popViewControllerAnimated:YES]; 95 } 96 97 - (void)more 98 { 99 [self popToRootViewControllerAnimated:YES]; 100 } 101 102 103 - (void)viewDidLoad { 104 [super viewDidLoad]; 105 // Do any additional setup after loading the view. 106 } 107 108 - (void)didReceiveMemoryWarning { 109 [super didReceiveMemoryWarning]; 110 // Dispose of any resources that can be recreated. 111 } 112 113 /* 114 #pragma mark - Navigation 115 116 // In a storyboard-based application, you will often want to do a little preparation before navigation 117 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 118 // Get the new view controller using [segue destinationViewController]. 119 // Pass the selected object to the new view controller. 120 } 121 */ 122 123 @end