Iurii Ushakov Iurii Ushakov - 4 years ago 154
Swift Question

Navigation Bar color is not saving

I have view controllers with two different navigation bars. Every navigation bar has different color.

VC1 and VC2

If I move from VC1 to VC2 I will see different colors but if move back I will see in VC1 navigation bar's color from VC2.

View Controller 1 Returned

So navigation bar color from VC1 is not saving properly

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()
}


How to make fixed navigation bar color in VC1? Thank you for help!

Answer Source

Instead of changing the color of the navigation bar in the viewDidLoad, do it in 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)
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download