## 一:效果
第二篇里面寫了怎樣自定義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;
~~~
- 前言
- (1)微博主框架-子控制器的添加
- (2)微博主框架-自定義導航控制器NavigationController
- (3)微博主框架-UIImage防止iOS7之后自動渲染_定義分類
- (4)微博自定義tabBar中間的添加按鈕
- (5)微博自定義搜索框searchBar
- (6)導航控制器NavigationController 的滑動回退功能實現
- (7)程序啟動新特性用UICollectionViewController實現
- (8)用AFNetworking和SDWebImage簡單加載微博數據
- (9)微博模型之時間相關重要操作,判斷剛剛,昨天,今年等等
- (10)微博cell中圖片的顯示以及各種填充模式簡介
- (11)發送微博自定義TextView實現帶占位文字
- (12)發送微博自定義工具條代理實現點擊事件
- (13)發送微博調用相機里面的圖片以及調用相機