自定义NavigationController导航栏
eg.
CZNavController.h
#import <UIKit/UIKit.h>
@interface CZNavController : UINavigationController
@end
CZNavController.m
#import "CZNavController.h"
#import "UIImage+Ex.h"
@interface CZNavController ()
@end
@implementation CZNavController
//此方法,会在CZNavController当前类,执行第一个方法之前先会执行一次,并且只会调用一次
+ (void)initialize{
//设置导航条的样式
UINavigationBar *navBar = [UINavigationBar appearance];
//UIBarMetricsDefault 背景图片 在横竖屏都显示
[navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
//设置标题的颜色
[navBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//重写导航控制器的push方法,每一个子控制器在跳转的时候都会调用此方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//viewController 就是子控制器,设置子控制器的自定义后退按钮
//1 自定义后退按钮
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"NavBack"] originalImage] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
//
UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItem.width = -10;
viewController.navigationItem.leftBarButtonItems = @[fixedItem,backItem];
//自定义后退按钮后,手势返回上一级控制器的功能恢复
self.interactivePopGestureRecognizer.delegate = nil;
//当push的时候隐藏tabBar
viewController.hidesBottomBarWhenPushed = YES;
//真正的做了控制器之间的跳转
[super pushViewController:viewController animated:animated];
}
- (void)backClick{
[self popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end