How to change navigation back button color from button tap

67 views Asked by At

I use a UISplitViewController and I change the back button color whenever showing the detail view through:

/* In ParentVC */

let backButton = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
backButton.tintColor = .red
self.navigationItem.backBarButtonItem = backButton

let vc = DetailVC()
self.splitViewController?.showDetailViewController(vc, sender: nil)

From inside the DetailVC the user is able to tap on a button to change the color of the VC, but the back button color is not changing. I found I can change the back button color by doing:

/* In DetailVC */

if let parentVC = self.splitViewController?.viewControllers.first?.children.first as? ParentVC {
     parentVC.navigationItem.backBarButtonItem?.tintColor = getNewColor()
}

However, the back button's color doesn't change to the first color chosen until the user taps the color change button a second time. It's always one tap behind, so if I choose a different color every time, it will always be set to the previous color.

Does anyone know how to force reload the navigation bar's back button? Or knows why this is happening?

Thank you in advance!

0

There are 0 answers