Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Navigation Bar Color

Is it possible to set the navigation bar color for just a single View Controller in the navigation hierarchy? Let the default navigation bar color be red and just the last view controller in the line should have a blue one. I've used these two lines to color the navigation bar of said view controller:

navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .white

But when going back (e.g. by pressing the back button) the navigation bar stays blue. Setting the color back to red using above code doesn't do anything.

like image 331
pmax1 Avatar asked Mar 07 '18 20:03

pmax1


People also ask

How do I change the color of the navigation bar in Swift?

navigationBar. barTintColor = UIColor. newBlueColor() and of course this just changes the colour of the navigation bar of the view controller that the code is within.

Why is navigation bar black?

One of the issues that can occur is coloring the nav bar when your keyboard is open for typing. To fix this, head into General Settings –> Color of Navigation Bar when Keyboard is opened. Set this color to black as well. This should fix any issues you may have when the keyboard is open.


2 Answers

The navigationBar is shared across all the view controllers that are in the same UINavigationController stack.

If you want to change it's look for a specific view controller, you'll have to set the new style when the view controller is shown and remove it when the view controller is dismissed. This could be done in the viewWillAppear/viewWillDisappear of your view controller for example.

like image 171
Ludovic Landry Avatar answered Sep 16 '22 16:09

Ludovic Landry


I could get the navigation bar to change colors coming from a ViewControllerB to ViewControllerA perfectly fine with your code. I am not sure what your initial problem was. Here is my code which worked:

ViewController A:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = .red
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonAction(_ sender: Any) {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "Second")
        //self.present(controller, animated: true, completion: nil)
        self.navigationController?.pushViewController(controller, animated: true)
    }


}

ViewController B:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.barTintColor = .blue
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

It worked without an issue.

like image 41
userx Avatar answered Sep 19 '22 16:09

userx