UINavigationBar tintColor is misbehaving

89 Views Asked by At

I have issue when set UINavigationBar tintColor.

It is misbehaving

I'm using xcode 11.3.1, swift 5, iOS 13.3

*MyClass

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.title = "test"
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
}

*result

  1. real device https://i.stack.imgur.com/TB3Sp.jpg

  2. simulator https://i.stack.imgur.com/RMV7n.jpg

I want to set back button color like title

I only get error on real device

Please help me

Thank you

3

There are 3 best solutions below

1
Ahmed Abd Elaziz On

To set back button and hide "back":

let backItem = UIBarButtonItem()
        backItem.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)
        navigationItem.backBarButtonItem = backItem

You should but this code to the ViewController before push the new one.

5
vincent On

Did you try using UIColor instead of #colorLiteral?

Like


self.navigationController?.navigationBar.tintColor = UIColor(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)

//or

self.navigationController?.navigationBar.tintColor = UIColor(displayP3Red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)


Edit: Since that the above did not work for you, did you try this?

self.navigationController?.navigationBar.tintColor = self.navigationController?.navigationItem.titleView?.backgroundColor

Considering that all you want is to have both the button and title to have the same colour right?

1
UnuSynth On

Try access to navigationController.navigationBar instead of navigationController.view :

    self.navigationController?.navigationBar.tintColor = #colorLiteral(red: 0.9708816409, green: 0.4246639013, blue: 0.3480253518, alpha: 1)