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"]];
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
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