在項(xiàng)目開發(fā)中,NavigationBar是經(jīng)常使用到的。不同的頁(yè)面,對(duì)NavigationBar的樣式要求也是不盡相同的。
可能需要“隱藏NavigationBar”、“透明的NavigationBar”、“跟隨滾動(dòng)變化NavigationBar的透明度”,
但是由于NavigationBar是UINavigationController下的viewControllers 共用的,再頁(yè)面轉(zhuǎn)場(chǎng)的動(dòng)畫中,就會(huì)出現(xiàn)各種過(guò)度不自然,例如黑色塊;
還有各種疏漏導(dǎo)致NavigationBar的樣式出現(xiàn)混亂。
1. 設(shè)置NavigationBar的顏色
//UIColor 轉(zhuǎn) UIImage
@implementation UIImage (Category)
+ (UIImage *)imageWithColor: (UIColor *) color{
CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
@end
//設(shè)置NavigationBar的顏色
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
//這里是設(shè)置紅色;如果設(shè)置透明顏色[UIColor clearColor]]
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forBarMetrics:UIBarMetricsDefault];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewDidAppear:animated];
//這里需要恢復(fù)NavigationBar在App的主顏色,不然下一個(gè)頁(yè)面也會(huì)是紅色
}
2. 隱藏NavigationBar
//當(dāng)前頁(yè)面設(shè)置隱藏,離開當(dāng)前頁(yè)面要恢復(fù)顯示,不然下一個(gè)頁(yè)面NavigationBar還是處于隱藏狀態(tài)
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewDidAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
小結(jié):如果你的項(xiàng)目里面是按上面的方式設(shè)置NavigationBar,很有可能會(huì)遺漏某些頁(yè)面,導(dǎo)致NavigationBar的樣式混亂,而且還會(huì)出現(xiàn)下面的問(wèn)題
問(wèn)題1. 在頁(yè)面返回的動(dòng)畫中,NavigationBar的區(qū)域變成黑色
第一個(gè)頁(yè)面設(shè)置NavigationBar隱藏,第二個(gè)頁(yè)面設(shè)置NavigationBar紅色
觀察下面動(dòng)畫,在第二個(gè)頁(yè)面返回來(lái)的時(shí)候,NavigationBar的區(qū)域變成黑色
問(wèn)題2. 在頁(yè)面返回的動(dòng)畫中,會(huì)顯示之前的顏色
第一個(gè)頁(yè)面設(shè)置NavigationBar透明,第二個(gè)頁(yè)面設(shè)置NavigationBar紅色
觀察下面動(dòng)畫,從第二個(gè)頁(yè)面返回到第一個(gè)頁(yè)面,紅色會(huì)短暫停留在第一個(gè)頁(yè)面
小結(jié):上面的第一個(gè)問(wèn)題,效果確實(shí)很丑;而且再加上各種設(shè)置、恢復(fù)設(shè)置,這些問(wèn)題確實(shí)困擾我了。有些情況沒(méi)辦法處理,就只能隱藏NavigationBar,自己再定制一個(gè)跟NavigationBar一樣高度的view用著,可以設(shè)置左邊按鈕,右邊按鈕,中間標(biāo)題,但樣式與位置還有用法,跟NavigationBar還是無(wú)法保持一致。
后來(lái)找到了一個(gè)第三方類庫(kù) RTRootNavigationController
這個(gè)項(xiàng)目提供一種透明的方式,讓開發(fā)者像以前一樣使用導(dǎo)航器,同時(shí),每個(gè) push 進(jìn)來(lái)的 VC 有自己獨(dú)立的導(dǎo)航條。
//引入RTRootNavigationController,頁(yè)面結(jié)構(gòu)層級(jí)會(huì)發(fā)生改變
RTRootNavigationController
`- RTContainerViewController
| `- RTContainerNavigationController
| `- YourViewController1
`- RTContainerViewController
`- RTContainerNavigationController
`- YourViewController2
//以前使用
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yourController];
//RTRootNavigationController用法
RTRootNavigationController *nav = [[RTRootNavigationController alloc] initWithRootViewController:yourController];
是不是非常簡(jiǎn)單,這樣每個(gè)頁(yè)面就有一個(gè)獨(dú)立的NavigationBar,不管你在頁(yè)面如何設(shè)置,都不會(huì)影響到第二個(gè)頁(yè)面,或者之前一個(gè)頁(yè)面。
對(duì)應(yīng)上面問(wèn)題1,問(wèn)題2,頁(yè)面跳轉(zhuǎn)的動(dòng)畫是不是比較正常了。
非常感謝作者 Ricky Tan 提供這么好的類庫(kù)。
我確實(shí)在網(wǎng)上找了很多類庫(kù),當(dāng)時(shí)都沒(méi)辦法達(dá)到這種效果。