navigation技巧
[A].獲取 導航欄所有的視圖控制器
獲取 導航欄所有的視圖控制器伍茄,選擇想要跳轉(zhuǎn)的那個視圖控制器 可直接傳值淹朋!無需考慮“反向傳值”F唱蒸!
展示例子??:
跳轉(zhuǎn)關系:
MyInfosViewController(我的信息) → VerifyIdentityViewController(驗證 舊手機號) → WriteNewPhoneNumberViewController(更改為新手機號)
各界面的層次關系:
修改舊的手機號成功!
跳轉(zhuǎn)到“我的信息”界面烛愧,并把新的手機號傳到“我的信息”界面油宜!
NSArray *pushVCAry = [self.navigationController viewControllers];
//pushVCAry.count-3 回到MyInfosVC中去
MyInfosViewController *popVC = [pushVCAry objectAtIndex:pushVCAry.count-3]; //(-1:當前層)
//可直接“進行傳值”
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]; } } //可直接“進行傳值” popVC.transmitPhoneNumberStr = _phoneNumTF.text; [self.navigationController popToViewController:popVC animated:YES];
[B].判斷在當前UIViewController中怜姿,viewWillDisappear的時候是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");
}
}
導航欄 navigationBar
-
系統(tǒng)自帶的圖標和文字的顏色:(
setTintColor
)[self.navigationController.navigationBar setTintColor:[UIColor whiteColor] ];
-
標題文字的設置:(富文本 ---
setTitleTextAttributes
)[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
-
半透明屬性設置
self.navigationController.navigationBar.translucent = NO;
在iOS 6之前(包括iOS 6) translucent默認就是NO慎冤,在iOS 7之后就默認是YES了。
[A].隱藏 導航欄邊框下面的黑線
一般:在“viewWillAppear”里(當前界面)沧卢,隱藏 導航欄邊下的黑線蚁堤;
在“viewWillDisappear”里(下個界面),再顯示 導航欄邊下的黑線但狭。
方法一
//隱藏 導航欄邊下的黑線 [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
方法二
//隱藏 導航欄邊下的黑線 self.navigationController.navigationBar.clipsToBounds = YES;
方法三
//獲取 某個視圖邊下的黑線 - (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; }
//獲取 導航欄邊下的黑線 UIImageView* blackLineImageView = [self getNavBlackWithBar:self.navigationController.navigationBar]; //去掉導航欄邊下的黑線 blackLineImageView.hidden = YES;
[B].返回按鈕
-
在自己這一層 呈队,設置 (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;
自帶返回的手勢!O艽荨粒竖!
有時候返回(設置了背景圖片)時,會產(chǎn)生卡頓<赣凇H锩纭!Q嘏怼朽砰!
-
解決:在(Push到的)子層 ,設置 返回按鈕:
//左側 返回按鈕 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; //點擊按鈕 (pop操作) [backButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside]; /** 設置 背景顏色:方便觀察Button的大小 */ //button.backgroundColor = [UIColor grayColor]; /** 設置 圖片 */ UIImage *imgForButton = [UIImage imageNamed:@"navbar_icon_back"]; [backButton setImage: imgForButton forState:UIControlStateNormal]; /** 設置 文字 */ 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); //硬編碼:設置UIButton位置喉刘、大小 //文本尺寸 CGSize titleLabelSize = [titleStr sizeWithAttributes:@{NSFontAttributeName:backButton.titleLabel.font}]; //圖片尺寸 CGSize buttonImageSize = imgForButton.size; //最終的寬度:文本尺寸的寬度+圖片尺寸的寬度 backButton.frame = CGRectMake(0,0,buttonImageSize.width + titleLabelSize.width, buttonImageSize.height); //左側的leftBarButtonItem瞧柔,使用返回按鈕 UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.navigationItem.leftBarButtonItem = barButtonItem;
隱藏返回按鈕
在上一層 視圖控制器 (無字,且 無點擊事件響應)
// 隱藏“返回”按鈕 可點擊(無字饱搏,且 無點擊事件響應) self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil]; //??將“返回”按鈕的文字設置不在界面顯示 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
在下一層 視圖控制器 (直接隱藏)
// 隱藏“返回”按鈕 [self.navigationItem setHidesBackButton:YES];
[C].設置 圖片背景 setBackgroundImage
使用的圖片:
“lucencyNavgationBar_ios7”:透明圖片非剃;
“navgationBar_ios7”:藍色漸變色圖片。
基本設置格式:
導航欄推沸、狀態(tài)欄(作為整體)顯示的都是圖片备绽!!/** 設置為圖片(狀態(tài)欄+導航欄) */ UIImage * Nav_BG_img = [[UIImage imageNamed:@"navgationBar_ios7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];//圖片 自適應大小 [self.navigationController.navigationBar setBackgroundImage:Nav_BG_img forBarMetrics:UIBarMetricsDefault];
效果:
錯誤展示:
導航欄背景圖片(backgroundImage) 設置為 無圖片的image 或 “
nil
”。//設置導航欄背景圖片 為 無圖片的image [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
或者:
//設置導航欄背景圖片 為 nil [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
效果:
總結
-
1.因為導航欄是同一個鬓催!而每個界面里導航欄的造型 不同肺素!
設置背景圖片,改變導航欄的外觀宇驾!需要對每個界面出現(xiàn)前(在“-(void)viewWillAppear:(BOOL)animated { }
”里)進行設置倍靡!需要“
-(void)viewWillAppear:(BOOL)animated { }
”、“-(void)viewWillDisappear:(BOOL)animated { }
”配合使用课舍!
-
2.使用圖片背景來設置界面里導航欄的造型時塌西,不能使用 系統(tǒng)自帶的“返回”按鈕,不然會出現(xiàn)“跳轉(zhuǎn)卡頓”的現(xiàn)象筝尾!
需在每個界面里捡需,設置各界面的“返回”按鈕!(在當前層里筹淫,設置 “返回”按鈕)
讓頁面導航欄變透明:(背景圖片 設置為透明圖片)
//透明的圖片 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"lucencyNavgationBar_ios7"] forBarMetrics:UIBarMetricsDefault]; //去掉 透明導航欄邊的黑邊 [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
效果:
查看視圖的層次關系:
一般操作:
在“
-(void)viewWillAppear:(BOOL)animated { }
”里面://透明的圖片 [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"lucencyNavgationBar_ios7"] forBarMetrics:UIBarMetricsDefault]; //去掉 透明導航欄邊的黑邊 [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
在“
-(void)viewWillDisappear:(BOOL)animated { }
”里面站辉,重置:[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; //顯示 透明導航欄邊的黑邊 [self.navigationController.navigationBar setShadowImage:nil];
對于push到的界面:
根據(jù)下個界面的導航欄造型是否改變!
在下個界面的“-(void)viewWillAppear:(BOOL)animated { }
”里,重新設置navigationBar的背景圖片J伟殊霞!
[D]. 隱藏導航欄
-
顯示 導航欄:
最佳:
[self.navigationController setNavigationBarHidden:NO animated:YES];
不建議:
self.navigationController.navigationBarHidden = NO;
或
self.navigationController.navigationBar.hidden = NO;
顯示導航欄 效果:
本層:當前界面
下一層:push到的界面
-
隱藏 導航欄:
最佳:
[self.navigationController setNavigationBarHidden:YES animated:YES];
不建議:
self.navigationController.navigationBarHidden = YES;
或
self.navigationController.navigationBar.hidden = YES;
隱藏導航欄 效果:
本層:當前界面
下一層:push到的界面
使用實例
僅僅在某個視圖控制器中,隱藏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]. 適配 導航欄
當前視圖控制器里的UIScrollView绷蹲,自動適配導航欄
self.automaticallyAdjustsScrollViewInsets = NO; //自動適配 導航欄
修改當前視圖控制器的“edgesForExtendedLayout
”屬性:
self.edgesForExtendedLayout = UIRectEdgeNone; //布局 從導航欄下面開始
將“edgesForExtendedLayout
”屬性 設置為UIRectEdgeNone
,那么布局就是從導航欄下面開始9帕ぁH秤摇!
[F]. 背景色設置
基本格式:“setBackgroundColor:
”
展示例子??:
[self.navigationController.navigationBar setBackgroundColor:[UIColor blueColor]];
UIImage * Nav_BG_img = [[UIImage imageNamed:@"navgationBar_ios7"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];//圖片 自適應大小
[self.navigationController.navigationBar setBackgroundImage:Nav_BG_img forBarMetrics:UIBarMetricsDefault]; //跳轉(zhuǎn)遲鈍
但是使用 設置“背景色”岩齿,需要配合設置 狀態(tài)欄的顏色!
barTintColor
涉及到
UIVisualEffectView
苞俘!
在界面跳轉(zhuǎn)時盹沈,導航欄顏色變化會有UIVisualEffectView
的效果!一般不設置“setBarTintColor
”吃谣。展示例子??:
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor] ];
“背景圖片(backgroundImage)”乞封、“barTintColor” 和 “背景色(backgroundColor)” 的區(qū)別:
barTintColor:紅色