1

I use this to set NavigationBarColor before it run:

 UINavigationBar.appearance().barTintColor = Color.NavigationBackground

But in the program,I want to change the NavigationBarColor, So I use this again

 UINavigationBar.appearance().barTintColor = Color.Black

But nothing happen, It still white(Color.Background)
Color is a struct that I defined.
How to change the color correctly? I want to achieve like this:Trying to reload view controller to update the current theme

  • Do you want to change navigation bar color or table view background???? – NickCoder Jun 10 '19 at 10:04
  • 2
    Possible duplicate of [Changing navigation bar color in Swift](https://stackoverflow.com/questions/24687238/changing-navigation-bar-color-in-swift) – NickCoder Jun 10 '19 at 10:13
  • UITableView,I call it NavigationBar. I want to achieve some kind like this,but it is not working.https://stackoverflow.com/questions/55697172/trying-to-reload-view-controller-to-update-the-current-theme –  Jun 10 '19 at 10:16
  • only navigation color or whole app color(that would be themes) – NickCoder Jun 10 '19 at 10:40
  • can you edit your question and briefly expalin what do you want to achieve – NickCoder Jun 10 '19 at 10:41
  • I found the solution, sorry I just paste the code wrong. –  Jun 10 '19 at 11:36
  • If you have answered your own question please mention your answer below and also if one of the below answers is correct please mark it as answered – NickCoder Jun 10 '19 at 12:25

5 Answers5

1

if you want each screen to have different color add below line with color of your choice in view will appear and it will change color for each screen.

Swift 4.2:

//To change Navigation Bar Background Color
UINavigationBar.appearance().barTintColor = UIColor.blue
//To change Back button title & icon color
UINavigationBar.appearance().tintColor = UIColor.white
//To change Navigation Bar Title Color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

add it in view will appear and then you can see it changing.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.barTintColor = .orange
}
NickCoder
  • 1,504
  • 2
  • 23
  • 35
0

if you want to change the color of navigation bar color

navigationController?.navigationBar.barTintColor = UIColor.black
Jins George
  • 121
  • 6
0

Use the appearance API, and barTintColor color.

UINavigationBar.appearance().barTintColor = UIColor.red
Deviyani Swami
  • 749
  • 8
  • 17
0

In your ViewController's viewWillAppear(_:) simply set navigationBar's barTintColor as your required color, i.e.

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

enter image description here

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

Use this code to set UINavigationBar color initially in your Appdelegate’s didFinishLaunchingWithOptions.

    UINavigationBar.appearance().isTranslucent = false
    UINavigationBar.appearance().backgroundColor = Color.Background
    UINavigationBar.appearance().barTintColor = Color.Background
    UINavigationBar.appearance().tintColor = UIColor.white

And when you want to change color of UINavigationBar within app, just use these lines of code. Let’s say change color is your button action.

    @IBAction func changeThemeColor(_ sender: UIButton) {

        self.navigationController?.navigationBar.backgroundColor = Color.Black
self.navigationController?.navigationBar.barTintColor = Color.Black
    }
VRAwesome
  • 4,721
  • 5
  • 27
  • 52