iosswiftuinavigationbar

Changing navigation bar color in Swift


I am using a Picker View to allow the user to choose the colour theme for the entire app.

I am planning on changing the colour of the navigation bar, background and possibly the tab bar (if that is possible).

I've been researching how to do this but can't find any Swift examples. Could anyone please give me an example of the code I would need to use to change the navigation bar colour and navigation bar text colour?

The Picker View is set up, I'm just looking for the code to change the UI colours.


Solution

  • Navigation Bar:

    navigationController?.navigationBar.barTintColor = UIColor.green
    

    Replace greenColor with whatever UIColor you want, you can use an RGB too if you prefer.

    Navigation Bar Text:

    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
    

    Replace orangeColor with whatever color you like.

    Tab Bar:

    tabBarController?.tabBar.barTintColor = UIColor.brown
    

    Tab Bar Text:

    tabBarController?.tabBar.tintColor = UIColor.yellow
    

    On the last two, replace brownColor and yellowColor with the color of your choice.