- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
ViewController *mainView = [[ViewController alloc]init];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainView];
navi.navigationBar.backgroundColor = [UIColor blueColor];
[self.window setRootViewController:navi];
[self.window makeKeyAndVisible];
return YES;
}
導(dǎo)航條的字體和顏色的設(shè)置self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; // --- 字體顏色
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BJ.png"] forBarMetrics:UIBarMetricsDefault]; // — 背景色
導(dǎo)航條跳轉(zhuǎn)頁面的考慮對于用navigationcontroller來跳轉(zhuǎn)頁面的時(shí)候,其實(shí)是執(zhí)行堆棧的進(jìn)棧和出棧的操作,要想釋放內(nèi)存,那么在來回跳轉(zhuǎn)的時(shí)候,就要考慮幾個(gè)問題了1 A =>B=>C=>D,D=>A 有根視圖的話 (HOME)[self.navigationController popToRootViewControllerAnimated:YES]; D=>C (每一個(gè)界面返回上一層)[self.navigationController popViewControllerAnimated:YES]; 返回到上一層,并且傳遞參數(shù)//此頁面已經(jīng)存在于self.navigationController.viewControllers中,并且是當(dāng)前頁面的前一頁面 CViewController *cvc = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];CViewController *cvc = [CViewController alloc]init];cvc.str = self.str;[self.navigationController popToViewController:cvc animated:true];返回到上一層后,上一頁面顯示后要接收參數(shù),并刷新。注意此時(shí)應(yīng)該在viewDidAppear中進(jìn)行判斷并接收傳遞的值-(void)viewDidAppear:(BOOL)animated{ //判斷并接收返回的參數(shù)}2 A =>B=>C=>D=>E,E=>B=>C=>E因?yàn)锽在之前已經(jīng)出現(xiàn)過,不能在E中直接PUSH到B,因?yàn)槟菢右呀?jīng)是兩個(gè)B了,增加內(nèi)存,所以在跳轉(zhuǎn)的時(shí)候,就要進(jìn)行判斷是否之前已經(jīng)出現(xiàn)過B了,出現(xiàn)過,則直接push。這樣push到的是原有的B,不會在內(nèi)存中重新生成一個(gè)B了。NSArray *array = self.navigationController.viewControllers;
for (UIViewController *vc in array) {
if ([vc isKindOfClass:[BXXXViewController class]]) {
push VC;
}
或者知道每個(gè)界面的指針
[self.navigationController
popToViewController: [self.navigationController.viewControllers
objectAtIndex: ([self.navigationController.viewControllers count] -4)]
animated:YES];
在使用時(shí),根據(jù)自己返回層的需要,只要改變一下“-4”這個(gè)數(shù)字就可以達(dá)到目的了
//=====2016年3月17日 增加導(dǎo)航條的變化 Xcode7.2
很多的應(yīng)用現(xiàn)在都做到了,隨著頁面的滑動導(dǎo)航條的顏色也會發(fā)生變化,現(xiàn)在使用原生的導(dǎo)航條來體現(xiàn)一下基本原理。。。
*在項(xiàng)目屬性里設(shè)置
View controller-based status bar appearance == NO 默認(rèn)是YES
才可以改變導(dǎo)航條上20像素位置的狀態(tài)欄顏色
干貨:
本例使用的 UITableView 添加KVO實(shí)現(xiàn)監(jiān)控滑動位置的變化
[_myTable addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionInitial context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ if ([keyPath isEqualToString:@"contentOffset"]) { [self navChange]; }}
-(void)navChange{ if (_myTable.contentOffset.y <= 64) { self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName]; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BJ1.png"] forBarMetrics:UIBarMetricsDefault]; self.title = @"初始導(dǎo)航條"; } else if (_myTable.contentOffset.y >= 192 ) { self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BJ2.png"] forBarMetrics:UIBarMetricsDefault]; self.title = @"導(dǎo)航條最終狀態(tài)"; // 狀態(tài)欄顏色為白色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; }else { self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName]; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BJ3.png"] forBarMetrics:UIBarMetricsDefault]; self.title = @"導(dǎo)航條中間狀態(tài)"; // 狀態(tài)欄顏色為白色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; }}
上效果圖 無圖無真相
demo地址:https://github.com/Lian1990/NavColorRamp
新聞熱點(diǎn)
疑難解答
圖片精選