2017-02-05 56 views
2

我有兩個不同導航欄的視圖控制器。每個導航欄都有不同的顏色。導航欄顏色不存儲

VC1 and VC2

如果我從VC1移動到VC2我會看到不同的顏色,但如果回遷,我會從VC2 VC1導航欄的顏色看。

View Controller 1 Returned

所以從VC1導航欄的顏色是不保存正確

VC1:

import UIKit 

class TableViewController_1: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.navigationController?.navigationBar.barTintColor = UIColor(red: 56.0/255.0, green: 208.0/255.0, blue: 125.0/255.0, alpha: 1.00) 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

VC2:

import UIKit 

class TableViewController_2: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.navigationController?.navigationBar.barTintColor = UIColor(red: 105.0/255.0, green: 28.0/255.0, blue: 56.0/255.0, alpha: 1.00) 

     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

如何使固定導航欄顏色VC1 ?謝謝你的幫助!

回答

1

而不是改變導航欄的顏色在viewDidLoad中,做到在viewWillAppear中:

VC1

override func viewWillAppear(_ animated: Bool) { 
     self.navigationController?.navigationBar.barTintColor = UIColor(red: 56.0/255.0, green: 208.0/255.0, blue: 125.0/255.0, alpha: 1.00) 
    } 

VC2

override func viewWillAppear(_ animated: Bool) { 
     self.navigationController?.navigationBar.barTintColor = UIColor(red: 105.0/255.0, green: 28.0/255.0, blue: 56.0/255.0, alpha: 1.00) 
    } 
+0

謝謝!它工作! –

1

如果你現在從VC1到VC2,他們有不同的導航控制器。所以,導航欄顏色不應該有任何問題。因爲他們使用不同的導航。但是,如果您從VC1推送到VC​​2,並且從VC2移回VC1時,應該在方法viewWillAppear中設置VC1的導航欄顏色。因爲當你移回到VC1時,由於已經在內存中創建了VC1,所以它會連續運行viewWillAppear,而不是viewDidLoad