## 一:添加導航控制器
上一篇博客完成了對底部的TabBar的設置,這一章我們完成自定義導航控制器(NYNavigationController)。
為啥要做自定義呢,因為為了更好地封裝代碼,并且系統的UINavigationController不能滿足我們的需求了,所以得自定義。
首先,我們在NYTabBarViewController的?
- (void)addChildVc:(UIViewController?*)childVc title:(NSString?*)title image:(NSString?*)image selectedImage:(NSString?*)selectedImage方法中寫了這個:
~~~
// 先給外面傳進來的小控制器 包裝 一個導航控制器
NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc];
// 添加為子控制器
[self addChildViewController:nav];
~~~
來給設置的各個Controller包裝一個導航控制器。
這時候系統會自動給添加一個。然后呢我們要對導航控制器進行改進。
### 框架結構
目前情況下的UI架構如下圖所示:一個IWTabBarController擁有4個導航控制器作為子控制器,每個導航控制器都有自己的根控制器(棧底控制器)?

* * *
### 重要代碼
1.給控制器包裝一個導航控制器并且放入tabbarController中
~~~
// 先給外面傳進來的小控制器 包裝 一個導航控制器
NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc];
// 添加為子控制器
[self addChildViewController:nav];
~~~
* * *
## 二:導航控制器左右item的設置
在NYMessageCenterViewController中我們添加了cell,并使之可以點擊,點擊后進入到另一個界面(test1) 再點擊界面的view進入另外一個界面(test2)
首先放入20行假數據——UITableView的數據源方法
返回一組,20行,每行內容cell設置
~~~
#pragma mark - Table view data source 數據源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"ID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = [NSString stringWithFormat:@"test~~~~message - %d", indexPath.row];
return cell;
}
~~~
然后是cell的點擊方法了 不用死記全部方法名字,簡單敲一下tableview 查找didSele方法(學iOS對英語挺高老快了)靈活運用xcode的自動提示功能。
~~~
#pragma mark - 代理方法
//cell的點擊事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NYTest1ViewController *test1 = [[NYTest1ViewController alloc] init];
test1.title = @"測試1控制器";
[test1.navigationController setNavigationBarHidden:NO];
[self.navigationController pushViewController:test1 animated:YES];
}
~~~
test1是我們自己做的一個測試類,其中我們做了兩個如圖:?

這時候,我們的消息界面就有了cell的數據并且可以點擊了。如圖效果:?
?
(到test2的push和1的一樣,不過是用的view的touch方法)
這時候我們要做導航控制器的左右item了。?
然后我們設置導航控制器的左右item (寫私信按鈕等)?
如圖:?
?
?

~~~
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)];
//設置右側按鈕為不可點擊狀態
self.navigationItem.rightBarButtonItem.enabled = NO;
NYLog(@"NYMessageCenterViewController-viewDidLoad");
}
~~~
其中的UIBarButtonItem 的創建方法不是系統給的,而是我們為了實現黃色的效果自己寫的分類實現的。
### 分類實現UIBarButtonItem的自定義創建方法:
~~~
//
// UIBarButtonItem+Extension.m
// 貓貓微博
//
// Created by apple on 15-6-4.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "UIBarButtonItem+Extension.h"
@implementation UIBarButtonItem (Extension)
/**
* 創建一個item
*
* @param target 點擊item后調用哪個對象的方法
* @param action 點擊item后調用target的哪個方法
* @param image 圖片
* @param highImage 高亮的圖片
*
* @return 創建完的item
*/
+(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage
{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//設置圖片
[backBtn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[backBtn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
[backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//設置尺寸
CGSize imageSize = backBtn.currentBackgroundImage.size;
backBtn.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
UIBarButtonItem *itemBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
return itemBtn;
}
@end
~~~
這里設置尺寸用到了
~~~
CGSize imageSize = backBtn.currentBackgroundImage.size;
~~~
在我們學習UI的transform的時候,我們知道 是不能直接這么設置size的,但是為啥這里能呢? 很簡單 ,我們對UIView寫了一個分類
~~~
//
// UIView+Extension.m
// 貓貓微博
//
// Created by apple on 15-6-2.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "UIView+Extension.h"
@implementation UIView (Extension)
-(void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
-(CGFloat)x
{
return self.frame.origin.x;
}
-(void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
-(CGFloat)y
{
return self.frame.origin.y;
}
-(void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
-(CGFloat)width
{
return self.frame.size.width;
}
-(void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
-(CGFloat)height
{
return self.frame.size.height;
}
-(void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
-(CGSize)size
{
return self.frame.size;
}
-(void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
-(CGPoint)origin
{
return self.frame.origin;
}
@end
~~~
?
?
并且為了改變系統原生的 美麗的藍色情調,換成微博的黃色。。。?

我們要重寫NYNavigationController初始加載方法 (initialize)以及重寫pushViewController方法,讓push 的時候會自動帶著箭頭按鈕和右邊的更多按鈕(UIBarButtonItem)

~~~
//
// NYNavigationController.m
// 貓貓微博
//
// Created by apple on 15-6-4.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYNavigationController.h"
@interface NYNavigationController ()
@end
@implementation NYNavigationController
+(void)initialize
{
// 設置整個項目所有item的主題樣式
UIBarButtonItem *item = [UIBarButtonItem appearance];
// 普通狀態
NSMutableDictionary *textAttrsNormal = [NSMutableDictionary dictionary];
textAttrsNormal[NSForegroundColorAttributeName] = [UIColor orangeColor];
textAttrsNormal[NSFontAttributeName] = [UIFont systemFontOfSize:14];
[item setTitleTextAttributes:textAttrsNormal forState:UIControlStateNormal];
// 不可用狀態
NSMutableDictionary *textAttrsDisabled = [NSMutableDictionary dictionary];
textAttrsDisabled[NSFontAttributeName] = [UIFont systemFontOfSize:14];
textAttrsDisabled[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
[item setTitleTextAttributes:textAttrsDisabled forState:UIControlStateDisabled];
}
/**
* 重寫這個方法目的:能夠攔截所有push進來的控制器
*
* @param viewController 即將push進來的控制器
*/
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// 這時push進來的控制器viewController,不是第一個子控制器(不是根控制器)
if (self.viewControllers.count > 0) {
/* 自動顯示和隱藏tabbar */
viewController.hidesBottomBarWhenPushed = YES;
// 設置左邊的箭頭按鈕
viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"navigationbar_back" highImage:@"navigationbar_back_highlighted"];
// 設置右邊的更多按鈕
viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"];
}
[super pushViewController:viewController animated:animated];
}
-(void)back
{
#warning 這里要用self,不是self.navigationController
// 因為self本來就是一個導航控制器,self.navigationController這里是nil的
[self popViewControllerAnimated:YES];
}
-(void)more
{
//回到根
[self popToRootViewControllerAnimated:YES];
}
@end
~~~
最后就是各個頁面的調用了?
首頁:?

~~~
- (void)viewDidLoad
{
[super viewDidLoad];
/* 設置導航欄上面的內容 */
self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(friendSearch) image:@"navigationbar_friendsearch" highImage:@"navigationbar_friendsearch_highlighted"];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(pop) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"];
}
~~~
我:?

~~~
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"設置" style:0 target:self action:@selector(setting)];
}
~~~
消息里面的寫私信(這里設置默認不可用狀態)?

~~~
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)];
//設置右側按鈕為不可點擊狀態
self.navigationItem.rightBarButtonItem.enabled = NO;
NYLog(@"NYMessageCenterViewController-viewDidLoad");
}
~~~
- 前言
- (1)微博主框架-子控制器的添加
- (2)微博主框架-自定義導航控制器NavigationController
- (3)微博主框架-UIImage防止iOS7之后自動渲染_定義分類
- (4)微博自定義tabBar中間的添加按鈕
- (5)微博自定義搜索框searchBar
- (6)導航控制器NavigationController 的滑動回退功能實現
- (7)程序啟動新特性用UICollectionViewController實現
- (8)用AFNetworking和SDWebImage簡單加載微博數據
- (9)微博模型之時間相關重要操作,判斷剛剛,昨天,今年等等
- (10)微博cell中圖片的顯示以及各種填充模式簡介
- (11)發送微博自定義TextView實現帶占位文字
- (12)發送微博自定義工具條代理實現點擊事件
- (13)發送微博調用相機里面的圖片以及調用相機