navigation技巧
[A].獲取 導(dǎo)航欄所有的視圖控制器
獲取 導(dǎo)航欄所有的視圖控制器,選擇想要跳轉(zhuǎn)的那個(gè)視圖控制器 可直接傳值!無需考慮“反向傳值”!!
展示例子??:
跳轉(zhuǎn)關(guān)系:
MyInfosViewController(我的信息) → VerifyIdentityViewController(驗(yàn)證 舊手機(jī)號(hào)) → WriteNewPhoneNumberViewController(更改為新手機(jī)號(hào))
各界面的層次關(guān)系:

修改舊的手機(jī)號(hào)成功!
跳轉(zhuǎn)到“我的信息”界面,并把新的手機(jī)號(hào)傳到“我的信息”界面!
NSArray *pushVCAry = [self.navigationController viewControllers];
//pushVCAry.count-3 回到MyInfosVC中去
MyInfosViewController *popVC = [pushVCAry objectAtIndex:pushVCAry.count-3]; //(-1:當(dāng)前層)
//可直接“進(jìn)行傳值”
popVC.transmitPhoneNumberStr = _phoneNumTF.text;
[self.navigationController popToViewController:popVC animated:YES];
也可以直接判斷
要跳轉(zhuǎn)回去的ViewController 是否為 “ MyInfosViewController”類型?NSArray *pushVCAry = [weakSelf.navigationController viewControllers]; UIViewController *popVC; for (int i = 0; i < pushVCAry.count; i ++) { if ([pushVCAry[i] isKindOfClass:[MyInfosViewController class] ]) { popVC = pushVCAry[i]; } } //可直接“進(jìn)行傳值” popVC.transmitPhoneNumberStr = _phoneNumTF.text; [self.navigationController popToViewController:popVC animated:YES];
[B].判斷在當(dāng)前UIViewController中,viewWillDisappear的時(shí)候是push還是pop:
- (void)viewWillDisappear:(BOOL)animated {
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
// View is disappearing because a new view controller was pushed onto the stack
NSLog(@"New viewcontroller was pushed");
} else if ([viewControllers indexOfObject:self] == NSNotFound) {
// View is disappearing because it was popped from the stack
NSLog(@"Viewcontroller was popped");
}
}
導(dǎo)航欄 navigationBar
-
系統(tǒng)自帶的圖標(biāo)和文字的顏色:(
setTintColor)[self.navigationController.navigationBar setTintColor:[UIColor whiteColor] ];
-
標(biāo)題文字的設(shè)置:(富文本 ---
setTitleTextAttributes)[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
-
半透明屬性設(shè)置
self.navigationController.navigationBar.translucent = NO;在iOS 6之前(包括iOS 6) translucent默認(rèn)就是NO,在iOS 7之后就默認(rèn)是YES了。
[A].隱藏 導(dǎo)航欄邊框下面的黑線
一般:在“viewWillAppear”里(當(dāng)前界面),隱藏 導(dǎo)航欄邊下的黑線;
在“viewWillDisappear”里(下個(gè)界面),再顯示 導(dǎo)航欄邊下的黑線。
方法一
//隱藏 導(dǎo)航欄邊下的黑線 [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
方法二
//隱藏 導(dǎo)航欄邊下的黑線 self.navigationController.navigationBar.clipsToBounds = YES;
方法三
//獲取 某個(gè)視圖邊下的黑線 - (UIImageView *)getNavBlackWithBar:(UIView *)bar { if ([bar isKindOfClass:[UIImageView class]] && bar.frame.size.height < 1) { return (UIImageView *)bar; } for (UIView *views in bar.subviews) { UIImageView *imageView = [self getNavBlackWithBar:views]; if (imageView) { return imageView; } } return nil; }//獲取 導(dǎo)航欄邊下的黑線 UIImageView* blackLineImageView = [self getNavBlackWithBar:self.navigationController.navigationBar]; //去掉導(dǎo)航欄邊下的黑線 blackLineImageView.hidden = YES;
[B].返回按鈕
-
在自己這一層 ,設(shè)置 (Push到的)子層的返回按鈕。
//自己的返回按鈕 self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"navbar_icon_back"];//圖片 UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil ]; self.navigationItem.backBarButtonItem = item;自帶返回的手勢(shì)?。。?br> 有時(shí)候返回(設(shè)置了背景圖片)時(shí),會(huì)產(chǎn)生卡頓!?。。?!
-
解決:在(Push到的)子層 ,設(shè)置 返回按鈕:
//左側(cè) 返回按鈕 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; //點(diǎn)擊按鈕 (pop操作) [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside]; /** 設(shè)置 背景顏色:方便觀察Button的大小 */ //button.backgroundColor = [UIColor grayColor]; /** 設(shè)置 圖片 */ UIImage *imgForButton = [UIImage imageNamed:@"navbar_icon_back"]; [backButton setImage: imgForButton forState:UIControlStateNormal]; /** 設(shè)置 文字 */ NSString *titleStr = @"返回"; [backButton setTitle:titleStr forState:UIControlStateNormal]; //字體大小 backButton.titleLabel.font = [UIFont systemFontOfSize:17.f]; //字體顏色 [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; /** 尺寸調(diào)整 */ backButton.frame = CGRectMake(0, 0 , 100, 100); //硬編碼:設(shè)置UIButton位置、大小 //文本尺寸 CGSize titleLabelSize = [titleStr sizeWithAttributes:@{NSFontAttributeName:backButton.titleLabel.font}]; //圖片尺寸 CGSize buttonImageSize = imgForButton.size; //最終的寬度:文本尺寸的寬度+圖片尺寸的寬度 backButton.frame = CGRectMake(0,0,buttonImageSize.width + titleLabelSize.width, buttonImageSize.height); //左側(cè)的leftBarButtonItem,使用返回按鈕 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.navigationItem.leftBarButtonItem = barButtonItem;
隱藏返回按鈕
在上一層 視圖控制器 (無字,且 無點(diǎn)擊事件響應(yīng))
// 隱藏“返回”按鈕 可點(diǎn)擊(無字,且 無點(diǎn)擊事件響應(yīng)) self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil]; //??將“返回”按鈕的文字設(shè)置不在界面顯示 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
在下一層 視圖控制器 (直接隱藏)
// 隱藏“返回”按鈕 [self.navigationItem setHidesBackButton:YES];
[C].設(shè)置 圖片背景 setBackgroundImage
使用的圖片:
“lucencyNavgationBar_ios7”:透明圖片;
“navgationBar_ios7”:藍(lán)色漸變色圖片。
基本設(shè)置格式:
導(dǎo)航欄、狀態(tài)欄(作為整體)顯示的都是圖片!!/** 設(shè)置為圖片(狀態(tài)欄+導(dǎo)航欄) */ UIImage * Nav_BG_img = [[UIImage imageNamed:@"navgationBar_ios7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];//圖片 自適應(yīng)大小 [self.navigationController.navigationBar setBackgroundImage:Nav_BG_img forBarMetrics:UIBarMetricsDefault];效果:
錯(cuò)誤展示:
導(dǎo)航欄背景圖片(backgroundImage) 設(shè)置為 無圖片的image 或 “
nil”。//設(shè)置導(dǎo)航欄背景圖片 為 無圖片的image [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];或者:
//設(shè)置導(dǎo)航欄背景圖片 為 nil [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];效果:
總結(jié)
-
1.因?yàn)?strong>導(dǎo)航欄是同一個(gè)!而每個(gè)界面里導(dǎo)航欄的造型 不同!
設(shè)置背景圖片,改變導(dǎo)航欄的外觀!需要對(duì)每個(gè)界面出現(xiàn)前(在“-(void)viewWillAppear:(BOOL)animated { }”里)進(jìn)行設(shè)置!需要“
-(void)viewWillAppear:(BOOL)animated { }”、“-(void)viewWillDisappear:(BOOL)animated { }”配合使用!
-
2.使用圖片背景來設(shè)置界面里導(dǎo)航欄的造型時(shí),不能使用 系統(tǒng)自帶的“返回”按鈕,不然會(huì)出現(xiàn)“跳轉(zhuǎn)卡頓”的現(xiàn)象!
需在每個(gè)界面里,設(shè)置各界面的“返回”按鈕!(在當(dāng)前層里,設(shè)置 “返回”按鈕)
讓頁面導(dǎo)航欄變透明:(背景圖片 設(shè)置為透明圖片)
//透明的圖片 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"lucencyNavgationBar_ios7"] forBarMetrics:UIBarMetricsDefault]; //去掉 透明導(dǎo)航欄邊的黑邊 [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];效果:
查看視圖的層次關(guān)系:
一般操作:
在“
-(void)viewWillAppear:(BOOL)animated { }”里面://透明的圖片 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"lucencyNavgationBar_ios7"] forBarMetrics:UIBarMetricsDefault]; //去掉 透明導(dǎo)航欄邊的黑邊 [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];在“
-(void)viewWillDisappear:(BOOL)animated { }”里面,重置:[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; //顯示 透明導(dǎo)航欄邊的黑邊 [self.navigationController.navigationBar setShadowImage:nil];對(duì)于push到的界面:
根據(jù)下個(gè)界面的導(dǎo)航欄造型是否改變!
在下個(gè)界面的“-(void)viewWillAppear:(BOOL)animated { }”里,重新設(shè)置navigationBar的背景圖片?。?/p>
[D]. 隱藏導(dǎo)航欄
-
顯示 導(dǎo)航欄:
最佳:
[self.navigationController setNavigationBarHidden:NO animated:YES];
不建議:
self.navigationController.navigationBarHidden = NO;
或
self.navigationController.navigationBar.hidden = NO;顯示導(dǎo)航欄 效果:
本層:當(dāng)前界面
下一層:push到的界面
-
隱藏 導(dǎo)航欄:
最佳:
[self.navigationController setNavigationBarHidden:YES animated:YES];
不建議:
self.navigationController.navigationBarHidden = YES;
或
self.navigationController.navigationBar.hidden = YES;隱藏導(dǎo)航欄 效果:
本層:當(dāng)前界面
下一層:push到的界面
使用實(shí)例
僅僅在某個(gè)視圖控制器中,隱藏navigationBar!
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//隱藏 navigationBar
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//顯示 navigationBar
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
效果:

[E]. 適配 導(dǎo)航欄
當(dāng)前視圖控制器里的UIScrollView,自動(dòng)適配導(dǎo)航欄
self.automaticallyAdjustsScrollViewInsets = NO; //自動(dòng)適配 導(dǎo)航欄
修改當(dāng)前視圖控制器的“edgesForExtendedLayout”屬性:
self.edgesForExtendedLayout = UIRectEdgeNone; //布局 從導(dǎo)航欄下面開始
將“edgesForExtendedLayout”屬性 設(shè)置為UIRectEdgeNone,那么布局就是從導(dǎo)航欄下面開始?。?!
[F]. 背景色設(shè)置
基本格式:“setBackgroundColor:”
展示例子??:
[self.navigationController.navigationBar setBackgroundColor:[UIColor blueColor]];
UIImage * Nav_BG_img = [[UIImage imageNamed:@"navgationBar_ios7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];//圖片 自適應(yīng)大小
[self.navigationController.navigationBar setBackgroundImage:Nav_BG_img forBarMetrics:UIBarMetricsDefault]; //跳轉(zhuǎn)遲鈍

但是使用 設(shè)置“背景色”,需要配合設(shè)置 狀態(tài)欄的顏色!
barTintColor
涉及到
UIVisualEffectView!
在界面跳轉(zhuǎn)時(shí),導(dǎo)航欄顏色變化會(huì)有UIVisualEffectView的效果!一般不設(shè)置“setBarTintColor”。展示例子??:
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor] ];
“背景圖片(backgroundImage)”、“barTintColor” 和 “背景色(backgroundColor)” 的區(qū)別:
barTintColor:紅色










