1.创建控制器BaseNavigationController继承自UINavigationController:
#import "BaseNavigationController.h"
@interface BaseNavigationController ()<UINavigationControllerDelegate>
@end
@implementation BaseNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.delegate = self;
//设置navigationBar颜色
[self.navigationBar setBackgroundImage:[UIImage imageWithColor:App_Main_Color rect:CGRectMake(0, 0, UI_SCREEN_WIDTH, HeightForNagivationBarAndStatusBar)] forBarMetrics:UIBarMetricsDefault];
//毛玻璃效果
self.navigationBar.translucent = NO;
//设置标题的样式
[self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
#pragma mark - 控制器视图将要出现时
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//通过对当前navigationController中viewControllers的数量来判断是否需要隐藏tabBar
if (self.viewControllers.count > 1) {
self.tabBarController.tabBar.hidden = YES;
}else {
self.tabBarController.tabBar.hidden = NO;
}
}
2.创建BaseViewControllerk继承自UIViewController:
#import "BaseViewController.h"
@interface BaseViewController ()
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
//判断是否有上级页面来确定是否显示返回按钮
if (self.navigationController.viewControllers.count > 1) {
[self setLeftBarButton];
}
}
#pragma mark - 自定义返回按钮
- (void)setLeftBarButton{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon-fh"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonClick)];
}
#pragma mark - 返回按钮点击事件
- (void)leftBarButtonClick{
[self.navigationController popViewControllerAnimated:YES];
}
3.在AppDelegate中对上述两个基类简单实用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
Item1ViewController *v1 = [[Item1ViewController alloc]init];
BaseNavigationController *n1 = [[BaseNavigationController alloc]initWithRootViewController:v1];
Item2ViewController *v2 = [[Item2ViewController alloc]init];
BaseNavigationController *n2 = [[BaseNavigationController alloc]initWithRootViewController:v2];
self.tabBarController = [[BaseTabBarController alloc]init];
self.tabBarController.viewControllers = @[n1,n2];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
item1.title = @"item1";
UITabBarItem *item2 = [tabBar.items objectAtIndex:1];
item2.title = @"item2";
self.window.rootViewController = self.tabBarController;
return YES;
}