一、新建一个控制器,继承UINavigationController
二、右滑手势代码
- (void)viewDidLoad{ [super viewDidLoad]; // 添加右滑手势 [self addSwipeRecognizer];}#pragma mark 添加右滑手势- (void)addSwipeRecognizer{ // 初始化手势并添加执行方法 UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(return)]; // 手势方向 swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight; // 响应的手指数 swipeRecognizer.numberOfTouchesRequired = 1; // 添加手势 [[self view] addGestureRecognizer:swipeRecognizer];}#pragma mark 返回上一级- (void)return{ // 最低控制器无需返回 if (self.viewControllers.count <= 1) return; // pop返回上一级 [self popToRootViewControllerAnimated:YES];}
三、然后只要在AppDelegate中将自定义的导航控制器设置为根控制器
#import "AppDelegate.h"#import "MainViewController.h"#import "FirstViewController.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // 初始化一个控制器 FirstViewController *first = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; // 初始化自定义的导航控制器 MainViewController *main = [[MainViewController alloc] initWithRootViewController:first]; // 把自定义的导航控制器设置为根控制器 self.window.rootViewController = main; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES;}
四、统一成一个导航控制器可以统一一些东西
1、统一导航栏样式
self.navigationBar.barTintColor = [UIColor whiteColor];
2、若在控制器之间跳转时需要做一些事情,可在自定义的控制器里添加下面两个方法
#pragma mark push方法- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{ // do something you want ... [super pushViewController:viewController animated:animated];}#pragma mark pop方法- (UIViewController *)popViewControllerAnimated:(BOOL)animated{ // 比如停止网络请求 ... return [super popViewControllerAnimated:animated];}