2016-04-30 46 views
1

我正在開發一個應用程序,其中應用程序的主頁具有隱藏細線的白色導航欄,其餘頁面具有綠色導航欄。到目前爲止,我所做的是將主頁導航欄的代碼放在自己的.swift文件中,但是當我通過主頁上的按鈕導航到其他頁面時(與菜單欄相反) )導航欄仍然設置爲白色。我認爲這是因爲當通過按鈕訪問時,刪除細線的代碼會傳遞到其他頁面。如何在Swift中爲不同頁面撤消發條刪除?

這是我的網頁應該非常像: Homepage & Module

這實際上是模塊頁面的樣子:Module Page Now

這是我使用的代碼 - 不知道是否有人能幫助我弄清楚如何反轉代碼或者是否有其他解決方案。

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    // Set navigation bar tint/background colour 
    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor() 

    // Set Navigation bar Title colour 
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()] 

    //Set navigation bar Back button tint colour 
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor() 

    //Get Rid of 1px hairline 
    self.navigationController?.navigationBar.setBackgroundImage(
     UIImage(), 
     forBarPosition: .Any, 
     barMetrics: .Default) 

    self.navigationController?.navigationBar.shadowImage = UIImage() 

} 

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 

    // Set navigation bar tint/background colour 
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 0/255, green: 127/255, blue: 106/255, alpha: 1) 

    // Set Navigation bar Title colour 
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] 

    //Set navigation bar Back button tint colour 
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() 

    //Reverse get Rid of 1px hairline code 
    /*self.navigationController?.navigationBar.setBackgroundImage(
     UIImage(), 
     forBarPosition: .Any, 
     barMetrics: .Default) 

    self.navigationController?.navigationBar.shadowImage = UIImage()*/ 
} 
+0

你是什麼意思爲「髮際線」? –

+0

@AlessandroOrnano與導航欄底部相鄰的1px線 – zenpain

回答

0

能夠通過反轉viewWillDisappear方法中的代碼來解決此問題。

//Reverse get Rid of 1px hairline code 
self.navigationController?.navigationBar.setBackgroundImage(nil, forBarMetrics: .Default) 
0

你可以用一般的方法(稱爲例如Utils.swift)的自定義快捷文件,並插入你的函數,然後調用它在未來的控制器:

import Foundation 
import UIKit 

//MARK: - General methods: 
    func customizeNavigationController(navigationController:UINavigationController) { 
      navigationController.navigationBar.backgroundColor = UIColor.greenColor() 
      navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) 
      navigationController.navigationBar.barTintColor = UIColor.greenColor() 
      navigationController.navigationBar.tintColor = UIColor.blackColor() 
      navigationController.navigationBar.barStyle = UIBarStyle.Black 
      navigationController.navigationBar.translucent = false 
      navigationController.navigationBar.clipsToBounds = false 
      navigationController.navigationBar.shadowImage = nil 
      self.setNeedsStatusBarAppearanceUpdate() 
     } 

     func changeNavHairLines(navigationController:UINavigationController, show:Bool) { 
      for subview in navigationController.navigationBar.subviews { 
       for view in subview.subviews { 
        if(view.frame.size.height < 4) { 
         if show == true { 
          view.alpha = 1.0 
         } else { 
          view.alpha = 0.0 
         } 
        } 
       } 
      } 
     } 

要調用它使用:

override func viewWillDisappear(animated: Bool) { 
    super.viewWillDisappear(animated) 
    // choose if you want to show or hide hairLines 
    changeNavHairLines(self.navigationController!,show:false) 
    customizeNavigationController(self.navigationController!) 
} 
+0

嗯,我很高興將邊框細線保留在除主頁外的其他頁面上。如果在加載主頁時已經觸發設置背景的代碼,我該如何擺脫其他頁面上的背景圖像/陰影圖像?對不起,如果這沒有意義。 – zenpain

+0

好吧,看看現在,我已經添加了一個函數來刪除這個奇怪的行 –

0
  1. 爲了除去細黑線...在viewDidLoad和的viewWillAppear相關視圖您想要移除細黑線的位置:

    //刪除導航欄底部的細黑線。

    for parent in self.navigationController!.navigationBar.subviews { 
         for childView in parent.subviews { 
          if(childView is UIImageView) { 
           childView.removeFromSuperview() 
          } 
         } 
        } 
    
  2. 對於不同的導航欄的外觀和它們的顏色和相關性能等爲什麼不自定義每個視圖的導航欄屬性,你做你的「主頁」?

+0

我試過了,但是「//取得1px髮際線」的代碼通過主頁按鈕導航到其他頁面 - 導致導航欄仍然是白色/透明的。我不知道如何撤消這個代碼? – zenpain

0

navigationController中的navigationBar在整個應用程序中共享,所以在一個控制器中刪除它將刪除整個App。如何隱藏導航欄在你想要刪除髮際線並通過Interface Builder手動放置導航條的控制器中?

可以隱藏的導航欄,返回到主視圖時:在viewWillAppear

self.navigationController?.setNavigationBarHidden(true, animated: true) 

廣場這一點。

然後再次顯示:

self.navigationController?.setNavigationBarHidden(false, animated: true) 

viewWillDisappear

+0

是的,這就是我想要做的。現在發生的事情是,當通過菜單欄訪問時,我的模塊頁面navigationBar顯示正常。但是,當通過主頁上的按鈕訪問時,導航仍然隱藏...我想我需要通過viewWillDisappear撤消隱藏位,但我不知道如何。 – zenpain