- (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;
}
導航條的字體和顏色的設置self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; // --- 字體顏色
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BJ.png"] forBarMetrics:UIBarMetricsDefault]; // — 背景色
導航條跳轉頁面的考慮對于用navigationcontroller來跳轉頁面的時候,其實是執行堆棧的進棧和出棧的操作,要想釋放內存,那么在來回跳轉的時候,就要考慮幾個問題了1 A =>B=>C=>D,D=>A 有根視圖的話 (HOME)[self.navigationController popToRootViewControllerAnimated:YES]; D=>C (每一個界面返回上一層)[self.navigationController popViewControllerAnimated:YES]; 返回到上一層,并且傳遞參數//此頁面已經存在于self.navigationController.viewControllers中,并且是當前頁面的前一頁面 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];返回到上一層后,上一頁面顯示后要接收參數,并刷新。注意此時應該在viewDidAppear中進行判斷并接收傳遞的值-(void)viewDidAppear:(BOOL)animated{ //判斷并接收返回的參數}2 A =>B=>C=>D=>E,E=>B=>C=>E因為B在之前已經出現過,不能在E中直接PUSH到B,因為那樣已經是兩個B了,增加內存,所以在跳轉的時候,就要進行判斷是否之前已經出現過B了,出現過,則直接push。這樣push到的是原有的B,不會在內存中重新生成一個B了。NSArray *array = self.navigationController.viewControllers;
for (UIViewController *vc in array) {
if ([vc isKindOfClass:[BXXXViewController class]]) {
push VC;
}
或者知道每個界面的指針
[self.navigationController
popToViewController: [self.navigationController.viewControllers
objectAtIndex: ([self.navigationController.viewControllers count] -4)]
animated:YES];
在使用時,根據自己返回層的需要,只要改變一下“-4”這個數字就可以達到目的了
//=====2016年3月17日 增加導航條的變化 Xcode7.2
很多的應用現在都做到了,隨著頁面的滑動導航條的顏色也會發生變化,現在使用原生的導航條來體現一下基本原理。。。
*在項目屬性里設置
View controller-based status bar appearance == NO 默認是YES
才可以改變導航條上20像素位置的狀態欄顏色
干貨:
本例使用的 UITableView 添加KVO實現監控滑動位置的變化
[_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 = @"初始導航條"; } 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 = @"導航條最終狀態"; // 狀態欄顏色為白色 [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 = @"導航條中間狀態"; // 狀態欄顏色為白色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; }}
上效果圖 無圖無真相
demo地址:https://github.com/Lian1990/NavColorRamp
新聞熱點
疑難解答