19.導(dǎo)航欄顏色的更改

  • 1.效果圖
市場_附近_美食_點(diǎn)餐.jpg

代碼:上面導(dǎo)航欄設(shè)置為了無色:透明

  • 在兩個方法里面寫四句代碼(思想:給導(dǎo)航欄設(shè)置一張對應(yīng)尺寸的全透明圖片即可。 )

    -(void)viewWillAppear:(BOOL)animated
    {
         [super viewWillAppear:animated];
    
         //設(shè)置導(dǎo)航欄背景圖片為一個空的image,這樣就透明了
         [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
    
         //去掉透明后導(dǎo)航欄下邊的黑邊
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
       [super viewWillDisappear:(BOOL)animated];
    
       //如果不想讓其他頁面的導(dǎo)航欄變?yōu)橥该?   [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
       [self.navigationController.navigationBar setShadowImage:nil];
    }
    
  • 2.改變導(dǎo)航欄的顏色(把下面的代碼下載你要改變的控制器里面)

改變導(dǎo)航欄的顏色
  //視圖將要顯示之前(這個顏色是導(dǎo)航欄的新的顏色)
  -(void)viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];

      self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
  }

  //視圖已經(jīng)消失(這個顏色是導(dǎo)航欄的原來的的顏色)
  -(void)viewWillDisappear:(BOOL)animated
  {
      [super viewWillDisappear:(BOOL)animated];

      self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
  }

代碼:改變顏色

  • 3.怎么改變導(dǎo)航欄上標(biāo)題的顏色和大小(2個方法)
  • <1>:(自定義視圖的方法)
    就是在導(dǎo)航向上添加一個titleView,可以使用一個label,再設(shè)置label的背景顏色透明,字體什么的設(shè)置就很簡單了。

    //自定義標(biāo)題視圖
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor grayColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor greenColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"新聞";
    self.navigationItem.titleView = titleLabel;
    
  • <2>:(在默認(rèn)顯示的標(biāo)題中直接修改文件的大小和顏色也是可以的)

     [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor redColor]}];
    

方式二相對于方式一而言更加簡單方便

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

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

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