您的位置:首页 > 其它

统一添加导航控制器右滑返回手势

2014-06-03 11:48 183 查看
一、新建一个控制器,继承UINavigationController





二、右滑手势代码

01
- (
void
)viewDidLoad
02
{
03
[super viewDidLoad];
04
 
05
// 添加右滑手势
06
[self addSwipeRecognizer];
07
}
08
09
#pragma mark 添加右滑手势
10
- (
void
)addSwipeRecognizer
11
{
12
// 初始化手势并添加执行方法
13
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(
return
)];
14
 
15
// 手势方向
16
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
17
 
18
// 响应的手指数
19
swipeRecognizer.numberOfTouchesRequired = 1;
20
 
21
// 添加手势
22
[[self view] addGestureRecognizer:swipeRecognizer];
23
}
24
25
#pragma mark 返回上一级
26
- (
void
)
return
27
{
28
// 最低控制器无需返回
29
if
(self.viewControllers.count <= 1)
return
;
30
 
31
// pop返回上一级
32
[self popToRootViewControllerAnimated:YES];
33
}
三、然后只要在AppDelegate中将自定义的导航控制器设置为根控制器

01
#import "AppDelegate.h"
02
#import "MainViewController.h"
03
#import "FirstViewController.h"
04
05
@implementation AppDelegate
06
07
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
08
{
09
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
10
11
// 初始化一个控制器
12
FirstViewController *first = [[FirstViewController alloc] initWithNibName:@
"FirstViewController"
bundle:nil];
13
 
14
// 初始化自定义的导航控制器
15
MainViewController *main = [[MainViewController alloc] initWithRootViewController:first];
16
 
17
// 把自定义的导航控制器设置为根控制器
18
self.window.rootViewController = main;
19
 
20
self.window.backgroundColor = [UIColor whiteColor];
21
[self.window makeKeyAndVisible];
22
return
YES;
23
}
四、统一成一个导航控制器可以统一一些东西

1、统一导航栏样式

1
self.navigationBar.barTintColor = [UIColor whiteColor];
2、若在控制器之间跳转时需要做一些事情,可在自定义的控制器里添加下面两个方法

01
#pragma mark push方法
02
- (
void
)pushViewController:(UIViewController *)viewController animated:(
BOOL
)animated
03
{
04
// do something you want
05
...
06
 
07
[super pushViewController:viewController animated:animated];
08
}
09
10
#pragma mark pop方法
11
- (UIViewController *)popViewControllerAnimated:(
BOOL
)animated
12
{
13
// 比如停止网络请求
14
...
15
 
16
return
[super popViewControllerAnimated:animated];
17
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: