AJ学IOS 之微博项目实战(6)导航控制器NavigationController 的滑动回退功能实现
2024-08-29 01:07:45
AJ分享,必须精品
一:效果
第二篇里面写了怎样自定义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;
最新文章
- node.js学习总结(一)
- MySQL大数据优化
- 洛谷P1372 又是毕业季I&;&;P1414 又是毕业季II[最大公约数]
- C#基础系列——委托实现简单设计模式
- ConvertHelper 通用类
- 贪心算法(Greedy Algorithm)
- SQL Server 2012 学习笔记1
- 单机安装HBase
- Android 编程下Touch 事件的分发和消费机制
- Erlang ODBC 处理中文
- Memcached缓存入门篇
- 花了一年时间开发的TTFEditor 字体编辑器
- tomcat-users.xml配置Manager登陆用户
- jQuery 中 jQuery(function(){})与(function(){})(jQuery) 的区别及用法
- 使用 Vue 和 epub.js 制作电子书阅读器
- 从网络上获取图片,并写入excel文件
- python入门(十七)python连接mysql数据库
- NET缓存框架CacheManager在混合式开发框架中的应用(1)-CacheManager的介绍和使用
- 全面理解Java内存模型(JMM)及volatile关键字(转载)
- python 报错RuntimeError: dictionary changed size during iteration
热门文章
- 小白学 Python 数据分析(21):pyecharts 好玩的图表(系列终篇)
- 试译 Understanding Delta-Sigma Modulators
- nginx 内存池
- cin.getline()的用法和坑
- Building Applications with Force.com and VisualForce (DEV401) 中用到的Recruiting Application介绍
- 《JavaScript 模式》读书笔记(5)— 对象创建模式4
- mybatis简单项目
- SQL实战(二)
- docker下安装centos,并在其上搭建lnmp环境
- 详细解析 HBASE 配置的各种要点