navigation's skills




navigation技巧

[A].獲取 導航欄所有的視圖控制器

獲取 導航欄所有的視圖控制器,選擇想要跳轉的那個視圖控制器直接傳值!無需考慮“反向傳值”??!

展示例子??:

跳轉關系:
MyInfosViewController(我的信息) → VerifyIdentityViewController(驗證 手機號) → WriteNewPhoneNumberViewController(更改手機號)

各界面的層次關系:



修改舊的手機號成功!
跳轉到“我的信息”界面,并把手機號傳到“我的信息”界面!

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];


也可以直接判斷
要跳轉回去的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;
    

    自帶返回手勢!??!
    有時候返回(設置背景圖片)時,會產(chǎn)生卡頓?。。。?!


  • 解決:在(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)“跳轉卡頓”的現(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背景圖片!!




[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,那么布局就是從導航欄下面開始?。?!




[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]; //跳轉遲鈍


背景圖片” 和 “背景色” 的區(qū)別

但是使用 設置“背景色”,需要配合設置 狀態(tài)欄顏色




barTintColor

涉及到UIVisualEffectView!


在界面跳轉時,導航欄顏色變化會有UIVisualEffectView的效果!一般不設置“setBarTintColor”。


展示例子??:
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor] ];


背景圖片(backgroundImage)”、“barTintColor” 和 “背景色(backgroundColor)” 的區(qū)別:

barTintColor:紅色
















goyohol's essay

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容