0

As the title implies I'm wondering if there is a way to change the background colour of a UINavigationBar in Swift. I know this question has been asked once before as I found this question here but I couldn't get any of the code to work when I tried to bring it up to date. I have linked the custom class which is a subclass of UINavigationController. Currently, the background colour of all the child UINavigationItem's have the default background colour of a slightly tinted white. I want to be able to change their colour to fit the rest of the UI elements in my app as most of them are extremely dark so I'm trying to change that below there is a picture of what I'm trying to replicate.

enter image description here

Is there is a more recent way to do this? Maybe using User defined runtime attributes. I'm out of ideas.

I've tried

@objc func changebarColor(){
        self.navigationController?.navigationBar.barTintColor = UIColor.red;
}

    override func viewDidLoad() {
        super.viewDidLoad()
        changebarColor()


}

And

override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent

        self.navigationController?.navigationBar.barTintColor  = UIColor.red;

}

And

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController!.navigationBar .setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController!.navigationBar.shadowImage = UIImage();
    self.navigationController!.navigationBar.isTranslucent = true;
    self.navigationController!.navigationBar.backgroundColor = UIColor.red;
}

As well as all of the other answers posted on the page. Slightly tweaking them to remove errors.

Standard
  • 45
  • 6
  • Maybe you should post the code you tried (and didn't work) for us to help you out best. If it worked then, it should work now. It's probably something trivial between versions. –  Sep 04 '18 at 02:20
  • Is your view controller in a navigation controller? Does the nav bar appear? What is the result of the code you've posted? – rmaddy Sep 04 '18 at 02:43
  • When you set the `barTintColor` what happened? Nothing? If so, do you _have_ a navigation controller? Do you have a navigation bar even? Show more context! – matt Sep 04 '18 at 02:46
  • If you have any other feedback feel free to comment it I'm new so I'm trying to work out how to phrase questions properly and appreciate your feedback @matt – Standard Sep 05 '18 at 01:24
  • But your screen shot shows a serious problem with your navigation bar. It isn’t up behind the status bar like its supposed to be. That suggest I’m right: the issue is that you have no navigation controller. So your commands all do nothing. You need to say much more about what class this code is in and why it has this incorrect navigation bar. – matt Sep 05 '18 at 01:50

1 Answers1

3

You are on the right track, but you are calling it in the wrong place. You should move it to viewWillAppear which is called every time you view is going to be displayed. viewDidLoad() is only called once.

This means that another view controller or even the OS can change the colour of your navigation bar after viewDidLoad()

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

Also, you don't need semi colons in Swift.

Brett
  • 1,647
  • 16
  • 34