1显蝌、navigationItem
navigation item在navigation Bar代表一個(gè)viewController坤检,具體一點(diǎn)兒來說就是每一個(gè)加到navigationController的viewController都 會有一個(gè)對應(yīng)的navigationItem凡泣,該對象由viewController以懶加載的方式創(chuàng)建峡扩,稍后我們可以在對象中對 navigationItem進(jìn)行配置踱蠢,可以設(shè)置leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title以及prompt等屬性汁汗。?前三個(gè)每一個(gè)都是一個(gè)UIBarButtonItem對象,最后兩個(gè)屬性是一個(gè)NSString類型描述妇斤,注意添加該 描述以后NavigationBar的高度會增加30摇锋,總的高度會變成74(不管當(dāng)前方向是Portrait還是Landscape,此模式下 navgationbar都使用高度44加上prompt30的方式進(jìn)行顯示)站超。當(dāng)然如果覺得只是設(shè)置文字的title不夠爽荸恕,你還可以通過 titleview屬性指定一個(gè)定制的titleview,這樣你就可以隨心所欲了死相,當(dāng)然注意指定的titleview的frame大小融求,不要顯示出界。
2算撮、titleTextAttributes(ios5.0以后可用)
這是UINavigationBar的一個(gè)屬性生宛,通過它你可以設(shè)置title部分的字體,這個(gè)屬性定義
NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor]; childOne.navigationController.navigationBar.titleTextAttributes = dict;?
3肮柜、wantsFullScreenLayout
viewController的一個(gè)屬性陷舅,這個(gè)屬性默認(rèn)值是NO,如果設(shè)置為YES的 話审洞,如果statusbar莱睁,navigationbar, toolbar是半透明的話,viewController的view就會縮放延伸到它們下面芒澜,但注意一點(diǎn)兒tabBar不在范圍內(nèi)仰剿,即無論該屬性是否為 YES,view都不會覆蓋到tabbar的下方痴晦。
4南吮、navigationBar中的stack
這個(gè)屬性可以算是UINavigationController的靈魂之一,它維護(hù)了一 個(gè)和UINavigationController中viewControllers對應(yīng)的navigationItem的stack阅酪,該stack用于 負(fù)責(zé)navigationbar的刷新旨袒。“注意:如果navigationbar中navigationItem的stack和對應(yīng)的 NavigationController中viewController的stack是一一對應(yīng)的關(guān)系术辐,如果兩個(gè)stack不同步就會拋出異常砚尽。
下面舉個(gè)簡單拋出異常的例子:
SvNavChildViewController *childOne = [[SvNavChildViewController alloc] initWithTitle:@"First" content:@"1"];
[self.navigationController pushViewController:childOne animated:NO];
[childOne release]; // raise exception when the stack of navigationbar and navigationController was not correspond [self.navigationController.navigationBar popNavigationItemAnimated:NO];
當(dāng)pushViewcontroller的之后,強(qiáng)制把navigationBar中的navigationItem pop一個(gè)出去辉词,程序立馬掛起必孤。當(dāng)然這純粹只是為了驗(yàn)證問題,一般不會有人這么寫
5瑞躺、navigationBar的刷新
通過前面介紹的內(nèi)容敷搪,我們知道navigationBar中包含了這幾個(gè)重要組成部 分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title。當(dāng)一個(gè)view controller添加到navigationController以后幢哨,navigationBar的顯示遵循一下幾個(gè)原則:
1)赡勘、Left side of the navigationBar
a)如果當(dāng)前的viewController設(shè)置了leftBarButtonItem,則顯示當(dāng)前VC所自帶的leftBarButtonItem捞镰。
b)如果當(dāng)前的viewController沒有設(shè)置 leftBarButtonItem闸与,且當(dāng)前VC不是rootVC的時(shí)候,則顯示前一層VC的backBarButtonItem岸售。如果前一層的VC沒有 顯示的指定backBarButtonItem的話践樱,系統(tǒng)將會根據(jù)前一層VC的title屬性自動(dòng)生成一個(gè)back按鈕,并顯示出來凸丸。
c)如果當(dāng)前的viewController沒有設(shè)置leftBarButtonItem拷邢,且當(dāng)前VC已是rootVC的時(shí)候,左邊將不顯示任何東西屎慢。
此處注意:5.0中新增加了一個(gè)屬性 leftItemsSupplementBackButton瞭稼,通過指定該屬性為YES,可以讓leftBarButtonItem和 backBarButtonItem同時(shí)顯示腻惠,其中l(wèi)eftBarButtonItem顯示在backBarButtonItem的右邊弛姜。
2)、title部分
a)如果當(dāng)前VC通過 .navigationItem.titleView指定了自定義的titleView妖枚,系統(tǒng)將會顯示指定的titleView廷臼,此處要注意自定義titleView的高度不要超過navigationBar的高度,否則會顯示出界绝页。
b)如果當(dāng)前VC沒有指定titleView荠商,系統(tǒng)則會根據(jù)當(dāng)前VC的title或者當(dāng) 前VC的navigationItem.title的內(nèi)容創(chuàng)建一個(gè)UILabel并顯示,其中如果指定了navigationItem.title的話续誉, 則優(yōu)先顯示navigationItem.title的內(nèi)容莱没。
3)、Right side of the navigationBar
a)如果當(dāng)前VC指定了rightBarButtonItem的話酷鸦,則顯示指定的內(nèi)容饰躲。
b)如果當(dāng)前VC沒有指定rightBarButtonItem的話牙咏,則不顯示任何東西。
6嘹裂、Toolbar
navigationController自帶了一個(gè)工具欄妄壶,通過設(shè)置 self.navigationController.toolbarHidden = NO來顯示工具欄,工具欄中的內(nèi)容可以通過viewController的toolbarItems來設(shè)置寄狼,顯示的順序和設(shè)置的NSArray中存放的順 序一致丁寄,其中每一個(gè)數(shù)據(jù)都一個(gè)UIBarButtonItem對象,可以使用系統(tǒng)提供的很多常用風(fēng)格的對象泊愧,也可以根據(jù)需求進(jìn)行自定義伊磺。
?7、UINavigationControllerDelegate
這個(gè)代理真的很簡單删咱,就是當(dāng)一個(gè)viewController要顯示的時(shí)候通知一下外面屑埋,給你一個(gè)機(jī)會進(jìn)行設(shè)置,包含如下兩個(gè)函數(shù): 當(dāng)你需要對某些將要顯示的viewController進(jìn)行修改的話痰滋,可實(shí)現(xiàn)該代理
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
?- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
**8雀彼、UINavigationController的viewControllers屬性
**
通過該屬性我們可以實(shí)現(xiàn)一次性替換整個(gè)navigationController的層次, 這個(gè)過程如果通過setViewControllers:animated:來設(shè)置,并指定動(dòng)畫為YES的畫即寡,動(dòng)畫將會從當(dāng)前的navigationController所顯示的vc跳轉(zhuǎn)到所設(shè)置的目標(biāo)viewController的最頂層的那個(gè)VC徊哑,而中間其他的VC將會被直接從VC層級中移除和添加進(jìn)來(沒有動(dòng)畫)。
9聪富、topViewController Vs visibleViewController
topViewController代表當(dāng)前navigation棧中最上層的VC莺丑,而 visibleViewController代表當(dāng)前可見的VC,它可能是topViewController墩蔓,也可能是當(dāng)前 topViewController present出來的VC梢莽。因此UINavigationController的這兩個(gè)屬性通常情況下是一樣,但也有可能不同奸披。
簡單的設(shè)置
navigationItem 凡是要設(shè)置item就可以直接通過navi噶他歐諾Item設(shè)置
navigationBar 導(dǎo)航條(0,20,375,44);默認(rèn)值
設(shè)置title
self.navigationItem.title = @"我是標(biāo)題";
self.title = @"我也是title";
自定義Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
titleLabel.textAlignment = NSTextAlignmentCenter;?
titleLabel.textColor = [UIColor redColor];?
titleLabel.text = @"自定義label";?
self.navigationItem.titleView = titleLabel;?
titleView 是可以添加別的控件
UISwitch *heheSwitch = [UISwitch new];
self.navigationItem.title = @"我又來了";?
中間天使兩個(gè)item 像[1|2]?
NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil nil nil];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
self.navigationItem.titleView = segment;
// 設(shè)置 文本的樣式?
NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:[UIColor redColor]};? [self.navigationController.navigationBar setTitleTextAttributes:dict];
修改返回按鈕
控制器 a push 到控制器b 中,想要修改返回按鈕的樣式就在控制器a中修改
如果通過這種方式設(shè)置返回樣式, 無法監(jiān)聽按鈕點(diǎn)擊, 被系統(tǒng)攔截掉
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(didClickButotn)];
過 這個(gè)屬性 backBarButtonItem 只能設(shè)置文本 self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil a
設(shè)置左右item
設(shè)置item?
UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[btn setImage:[UIImageimageNamed:@"kongxin"]forState:UIControlStateNormal];
[btn setImage:[UIImageimageNamed:@"shixin"]forState:UIControlStateSelected];
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:btn];
?
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(didClickButotn)];
? UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
UIBarButtonItem *right1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
self.navigationItem.rightBarButtonItems = @[right,right1];?
導(dǎo)航欄的設(shè)置?
// 背景顏色
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
// 設(shè)置導(dǎo)航欄主體顏色
? [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
只影響item UIBarButtonItemitem的,如果item為圖片會被渲染顏色, 默認(rèn)是藍(lán)色,
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
UIImage *iconImage = [UIImageimageNamed:@"navigationbar_friendsearch_highlighted"];
// 如果不想被渲染成 tintColor
UIImage *originImage = [iconImageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithImage:originImage style:UIBarButtonItemStylePlaintarget:nilaction:nil];?
只影響item UIBarButtonItem
[self.navigationController.navigationBarsetTintColor:[UIColorredColor]];
// 禁用掉高斯模糊, 磨玻璃效果
self.navigationController.navigationBar.translucent = NO;