負責整個項目的UINavigationController
不管是主流的tabbarController+NavigationController,還是獨立出來的NavigationController,整個項目中使用自定義的UINavigationController,方便管理修改。
下面的自定義的UINavigationController,解決了手勢返回的問題。
CJNavigationController.h
//
// CJNavigationController.h
// Personal Application
//
// Created by zengchunjun on 16/3/14.
// Copyright ? 2016年 zengchunjun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CJNavigationController : UINavigationController
//設置一個屬性保存系統(tǒng)的代理
@property (nonatomic, strong) id popDelegate;
@end
CJNavigationController.m
復制過去后報錯的地方自行修改。
//
// CJNavigationController.m
// Personal Application
//
// Created by zengchunjun on 16/3/14.
// Copyright ? 2016年 zengchunjun. All rights reserved.
//
#import "CJNavigationController.h"
#import "DiffUtil.h"
@interface CJNavigationController ()<UINavigationControllerDelegate>
@end
@implementation CJNavigationController
// 第一次用到這個類時使用這個方法,只執(zhí)行一次
+ (void)initialize{
[super initialize];
// 設置UIBarButtonItem的屬性
// 獲取當前類下得item
UIBarButtonItem *items = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
NSMutableDictionary *att = [NSMutableDictionary dictionary];
att[NSForegroundColorAttributeName] = [UIColor whiteColor];
// item字體的大小
att[NSFontAttributeName] = [DiffUtil getDifferFont:17];
[items setTitleTextAttributes:att forState:UIControlStateNormal];
//設置UINavigationBar的屬性
UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
bar.translucent = NO;
// UIImage *barImage = [UIImage imageWithColor:[DiffUtil getDifferColor] andSize:bar.bounds.size];
// [bar setBackgroundImage:barImage forBarMetrics:UIBarMetricsDefault];
// [bar setBarTintColor:[UIColor whiteColor]];//
[bar setBarTintColor:[DiffUtil getDifferColor]];
[bar setTintColor:[UIColor whiteColor]];
NSMutableDictionary *barAtt = [NSMutableDictionary dictionary];
// 設置標題的字體
barAtt[NSFontAttributeName] = [DiffUtil getDifferFont:17];
// 設置標題的顏色(前景色)
barAtt[NSForegroundColorAttributeName] = [UIColor whiteColor];
//設置item的顏色
[bar setTitleTextAttributes:barAtt];
// 設置狀態(tài)欄格式
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
// 推出下一個界面時都會調(diào)用這個方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
self.interactivePopGestureRecognizer.delegate = nil;
if (self.childViewControllers.count) {// 非根控制器
// 隱藏底部的tabbar
// viewController.hidesBottomBarWhenPushed = YES;
// 設置非控制器,左側(cè)返回右側(cè)返回根控制器按鈕圖片
UIImage *image = [[UIImage imageNamed:@"icon_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backPre)];
viewController.navigationItem.leftBarButtonItem = back;
}
// 調(diào)用父類的方法才會實現(xiàn)推出下一個界面的效果
[super pushViewController:viewController animated:YES];
}
// 返回上一個界面
- (void)backPre
{
[self popViewControllerAnimated:YES];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//判斷控制器是否為根控制器
if (self.childViewControllers.count) {
//將保存的代理賦值回去,讓系統(tǒng)保持原來的側(cè)滑功能
self.interactivePopGestureRecognizer.delegate = self.popDelegate;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
//將系統(tǒng)的代理保存(在view加載完畢就賦值--->viewDidLoad)
self.popDelegate = self.interactivePopGestureRecognizer.delegate;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end