应用场景:
1.当导航控制器push很多次,每个自控制器都需要自定义返回按钮,很麻烦
2.当进入二级界面以后,需要隐藏底部的tabbar
3.一次性设置顶部导航条的颜色
解决方法:
自定义导航控制器,重写push(跳到下一个控制器) 和 pop(返回上一个控制器) 方法
代码:
#import "SGNavigationController.h" @interface SGNavigationController () @end @implementation SGNavigationController - (void)viewDidLoad { [super viewDidLoad]; UINavigationBar *navBar = [UINavigationBar appearance]; //导航条背景色 UIColor *color = [[UIColor alloc] initWithRed:72/255.0 green:187/255.0 blue:211/255.0 alpha:1]; navBar.barTintColor = color; //字体颜色 navBar.tintColor = [UIColor whiteColor]; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ //返回按钮 UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init]; backItem.title = @"返回"; viewController.navigationItem.backBarButtonItem = backItem; //隐藏地步tabar if (self.childViewControllers.count){//即将跳往二级界面 self.navigationController.hidesBottomBarWhenPushed = YES; } [super pushViewController:viewController animated:YES]; } //弹回来 - (UIViewController *)popViewControllerAnimated:(BOOL)animated{ if (self.childViewControllers.count == 2) {//即将跳到根控制器 self.navigationController.hidesBottomBarWhenPushed = NO; } return [super popViewControllerAnimated:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
打包地址:
https://github.com/iOSSinger/SGNavigationController