【IOS開發基礎系列】Navigation頁面導航專題

IOS開發之UINavigationController詳解

http://www.open-open.com/lib/view/open1390611231914.html


1 原理

1.1 navigationController

????????對于父級VC與子級VC分別有navigationController的情況,即不是使用push方式加載子VC,而是通過AddChildViewController的方式添加的場景,則父級導航條會覆蓋在子級導航條上面,所以需要在載入時把父級導航條做隱藏處理:

1.2 navigationItem

????????在含有導航條的ViewController中,VC的navigationItem與VC.navigationController中的 navigationItem并不是同一個對象,如下圖所示。

1.3 示例

[self.flagshipStoreTabBarItem setTitle: @"發現"];

//??? [self.flagshipStoreTabBarItem setImage: [UIImage imageNamed: @"FhipedIcon"]];

[self.flagshipStoreTabBarItem setFinishedSelectedImage: [UIImage imageNamed: @"FhipedIcon"] withFinishedUnselectedImage: [UIImage imageNamed: @"FhipIcon"]];

UINavigationController view層級

2 開發技巧

2.1 iOS UINavigationController與UITabBarController的組合使用

http://sinye.iteye.com/blog/2093281

(Good)【IOS開發】UITabBarController和UINavigationController結合使用。

http://blog.sina.com.cn/s/blog_721cd3390101vr2d.html


2.2 初始化

2.2.1 初始化titleView

????????titleView要在具體ViewController中實現,而不是在NavigationViewController中實現。后者顯示不出來。

2.3 回退按鈕

2.3.1 回退按鈕自定義

UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @"BackNavIcon"] style: UIBarButtonItemStylePlain target: self action: @selector(backTo)];?

[backItem setTitle:@"Test"];

[self.navigationItem setLeftBarButtonItem: backItem];


錯誤做法:

UIBarButtonItem * backItem = [[UIBarButtonItem alloc] init];//BackNavIcon

[backItem setTitle: @"Test"];

[backItem setImage: [UIImage imageNamed: @"ForeIcon"]];

[self.navigationItem setBackBarButtonItem: backItem];


【iOS開發-22】navigationBar導航條和navigationItem設置:基本搞定導航條上的文字和按鈕以及各種跳轉

http://www.tuicool.com/articles/BZNVza


2.3.2 回退按鈕用圖標+文字

Creating a left-arrow button (like UINavigationBar's “back”style) on a UIToolbar

http://stackoverflow.com/questions/227078/creating-a-left-arrow-button-like-uinavigationbars-back-style-on-a-uitoolba


2.3.3 手動添加并呈現帶導航條的子VC

HJAddVC * addVC = [[HJAddVC alloc] init];

UINavigationController * addNavController = [[UINavigationController alloc] initWithRootViewController: addVC];

[addNavController addChildViewController: addVC];

[rootVC.view addSubview: addNavController.view];

[rootVC addChildViewController: addNavController];


[addVC.view setFrame: rootVC.view.frame];

[addVC.view setBackgroundColor: [UIColor whiteColor]];


2.3.4 手動移除帶導航條的子VC

視圖View與控制器都需要移除,缺一不可

- (void) closeView: (id)sender

{

??? [self.navigationController.view removeFromSuperview];

??? [self.navigationController removeFromParentViewController];

}


2.3.5 回退按鈕隱藏

[self.navigationController.navigationItem setHidesBackButton: YES];


2.4 導航條

2.4.1 透明導航條

//透明導航欄

[self.navigationController.navigationBar setBackgroundImage: pressedColorImg forBarMetrics: UIBarMetricsDefault];


2.5 常見開發問題

2.5.1 tableView被導航欄遮擋

IOS開發---菜鳥學習之路--(二十四)-iOS7View被導航欄遮擋問題的解決

http://www.cnblogs.com/PleaseInputEnglish/p/3498032.html


2.5.2 導航條設置為半透明

????????將NavigationBar設置透明(僅將指定視圖控制器進行透明處理),步驟如下:

????1.在視圖控制器的頭文件中實現UINavigationControllerDelegate,例如:

@interface PicturePreviewViewController: UIViewController

????2.在實現類中加入這個代理的方法及具體操作如下:

- (void) navigationController: (UINavigationController *)navigationController willShowViewController: (UIViewController*) viewController animated: (BOOL)animated{

? ? //如果進入的是當前視圖控制器

? ? if (viewController == self) {

? ? ? ? //背景設置為黑色

??????? self.navigationController.navigationBar.tintColor= [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];

? ? ? ? //透明度設置為0.3

??????? self.navigationController.navigationBar.alpha= 0.300;

???????//設置為半透明

???????self.navigationController.navigationBar.translucent = YES;?

? ? } else {

? ?????//進入其他視圖控制器

???????self.navigationController.navigationBar.alpha = 1;

?? ? ??//背景顏色設置為系統默認顏色 ??

???????self.navigationController.navigationBar.tintColor = nil;

? ? ? ?self.navigationController.navigationBar.translucent = NO;?

???}

}


navigationBar translucent

http://blog.csdn.net/yongyinmg/article/details/39957741


2.5.3 去掉回退按鈕文字

????????最近iOS項目中要求導航欄的返回按鈕只保留那個箭頭,去掉后邊的文字,在網上查了一些資料,最簡單且沒有副作用的方法就是:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment: UIOffsetMake(0, -60)

?forBarMetrics: UIBarMetricsDefault];

參考自這里:http://stackoverflow.com/questions/19078995/removing-the-title-text-of-an-ios-7-uibarbuttonitem


2.5.4 隱藏返回按鈕

[self.navigationItem setHidesBackButton: YES];


2.5.5 設置導航欄標題的字體顏色和大小

????方法一:(自定義視圖的方法,一般人也會采用這樣的方式)

????????就是在導航向上添加一個titleView,可以使用一個label,再設置label的背景顏色透明,字體什么的設置就很簡單了。

//自定義標題視圖

UILabel *titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 200, 44)];

titleLabel.backgroundColor = [UIColor grayColor];

titleLabel.font = [UIFont boldSystemFontOfSize: 20];

titleLabel.textColor = [UIColor greenColor];

titleLabel.textAlignment = NSTextAlignmentCenter;

titleLabel.text = @"新聞";

self.navigationItem.titleView = titleLabel;


????方法二:(在默認顯示的標題中直接修改文件的大小和顏色也是可以的)

[self.navigationController.navigationBar setTitleTextAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize: 19], NSForegroundColorAttributeName: [UIColor redColor]}];

????????方式二相對于方式一而言更加簡單方便


2.5.6 導航條上白色按鈕變成了藍色

//聲明這張圖片用原圖(別渲染),默認有亮藍色渲染

UIImage *img = [UIImage imageNamed: @"ShareIcon"];

img = [img imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];

_shareItem = [[UIBarButtonItem alloc] initWithImage: img style:UIBarButtonItemStylePlain target: self action: @selector(shareTo)];


2.5.7 TabBar與導航條混用時,TabBarItem的設置是在NavigationController中,而不是內容Controller中,切記!!!否則會導致頁面切換時選中狀態不準確

????????TabBar與導航條混用時,TabBarItem的設置是在NavigationController中,而不是內容Controller中,切記!!!否則會導致頁面切換時選中狀態不準確。


2.5.8 Tabbar的顯示與隱藏

Tabbar的隱藏函數,其實只在Nav Push的之前調用時起作用

//隱藏Tabbar

[viewController setHidesBottomBarWhenPushed: YES];

[super pushViewController: viewController animated: animated];


而Tabbar的顯示,則只有在Pop函數調用前執行才真正起作用

//顯示Tabbar

if ([viewController isKindOfClass: [RootVC class]]) {

??????? [viewController setHidesBottomBarWhenPushed: NO];

}


return [super popToViewController: viewController animated: animated];


3 參考鏈接

自定義iOS 7 導航欄背景,標題和返回按鈕文字顏色

http://blog.csdn.net/mad1989/article/details/41516743

IOS自定義導航欄題目和返回按鈕標題

http://blog.csdn.net/hengshujiyi/article/details/29864339

UINavigationBar自定義返回按鈕的設置

http://blog.sina.com.cn/s/blog_bf9843bf0101g01b.html

uibarbuttonitem image藍色

http://www.cocoachina.com/bbs/read.php?tid-180226-page-1.html

(good)iOS 7 UITabBar自定義選中圖片顯示為默認藍色的Bug

http://thierry-xing.iteye.com/blog/2171602

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,908評論 6 541
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,324評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,018評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,417評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,783評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,960評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,522評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,267評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,471評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,698評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,204評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,436評論 2 378

推薦閱讀更多精彩內容