猫猫学iOS 之微博项目实战(6)导航控制器NavigationController 的滑动回退功能实现
2015-07-30 13:17
751 查看
猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243
这里主要实现滑动回退功能
。
重写 UINavigationControllerDelegate 的方法
viewDidLoad中设置代理方法,并且预先设置手势代理用来还原
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243
一:效果
第二篇里面写了怎样自定义navigation实现自定义的导航控制器左右按钮样式,但是当我们自己实现后,系统自带的向右边滑动来实现回退的功能就不能用了。这里主要实现滑动回退功能
。
二:代码实现思路
首先 在 NYNavigationController.m中放一个popDelegate来放置要更改的手势代理对象@interface NYNavigationController ()<UINavigationControllerDelegate> @property (nonatomic, strong) id popDelegate; @end
重写 UINavigationControllerDelegate 的方法
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
viewDidLoad中设置代理方法,并且预先设置手势代理用来还原
- (void)viewDidLoad { [super viewDidLoad]; //记住手势代理 用来还原 _popDelegate = self.interactivePopGestureRecognizer.delegate; self.delegate = self; }
//导航控制器跳转完成的控制器 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController == self.viewControllers[0]) { // 是根控制器 //还原手势代理 self.interactivePopGestureRecognizer.delegate = _popDelegate; }else{ // 非根控制器 //设置手势代理为空,就可以实现滑动了 //实现滑动返回功能 //清空滑动返回手势的代理,就能实现滑动返回功能了。 self.interactivePopGestureRecognizer.delegate = nil; } }
三: 全部navigationController的代码
内部包括设置左右按钮等等功能//
// NYNavigationController.m
// 猫猫微博
//
// Created by apple on 15-7-29.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYNavigationController.h"
#import "UIBarButtonItem+Item.h"
@interface NYNavigationController ()<UINavigationControllerDelegate> @property (nonatomic, strong) id popDelegate; @end
@implementation NYNavigationController
+ (void)initialize
{
// 获取当前类下面的UIBarButtonItem
UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
// 设置导航条按钮的文字颜色 为***
NSMutableDictionary *titleAttr = [NSMutableDictionary dictionary];
titleAttr[NSForegroundColorAttributeName] = [UIColor orangeColor];
[item setTitleTextAttributes:titleAttr forState:UIControlStateNormal];
}
- (void)viewDidLoad { [super viewDidLoad]; //记住手势代理 用来还原 _popDelegate = self.interactivePopGestureRecognizer.delegate; self.delegate = self; }
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated];
// 设置非根控制器导航条内容
if (self.viewControllers.count != 0) { //非根控制器
//设置导航条的内容
//设置导航条左边和右边
//如果把导航条上的返回按钮覆盖了,那么就没有了滑动返回功能
//设置左边按钮
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_back"] highImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] target:self action:@selector(backToPre) forControlEvents:UIControlEventTouchUpInside];
//设置右边按钮
viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"navigationbar_more"] highImage:[UIImage imageNamed:@"navigationbar_more_highlighted"] target:self action:@selector(backToRoot ) forControlEvents:UIControlEventTouchUpInside];
}
}
-(void)backToPre{
//返回上一个控制器
[self popViewControllerAnimated:YES];
}
-(void)backToRoot{
//返回根控制器
[self popToRootViewControllerAnimated:YES];
}
#pragma mark - UINavigationControllerDelegate 实现滑动回退功能
//导航控制器跳转完成的控制器 - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (viewController == self.viewControllers[0]) { // 是根控制器 //还原手势代理 self.interactivePopGestureRecognizer.delegate = _popDelegate; }else{ // 非根控制器 //设置手势代理为空,就可以实现滑动了 //实现滑动返回功能 //清空滑动返回手势的代理,就能实现滑动返回功能了。 self.interactivePopGestureRecognizer.delegate = nil; } }
@end
四:注意
设置手势代理为空后必须要在该用的时候给设置回去,系统内部东西不能随便乱改,要么会出现难以预料的bug。在跟控制器的时候不小心做了回退滑动那样的操作会让再次进入下一个页面的导航控制器的右边按钮点击无效,app就崩溃了。self.interactivePopGestureRecognizer.delegate = nil;
相关文章推荐
- ios数据储存
- ios
- iOS应用崩溃日志揭秘
- iOS开发--使用OpenSSL生成私钥和公钥的方法
- 百度地图开发文档 iOS
- 自动释放池autoreleasepool
- iOS获取唯一标识
- IOS 实现加入购物车的效果
- IOS Delegate & protocal
- Can not find Storyboard ID in Identity inspector 找不到Storyboard ID
- iOS图片拉伸技巧
- ios开发学习笔记(转载)
- iOS开发系列--视图切换
- IOS应用内购功能
- IOS中使用像素位图(CGImageRef)对图片进行处理
- IOS TableView 根据子view 来获取当前Cell 方式
- iOS自定义cell导致数据加载混乱
- iOS 十进制转十六进制
- Ios开发之 -- js和ios的交互
- iOS 视图封装及适配