iOS 12 NavigationBar barButton turned gray when canceled a pop gesture

Viewed 490

There are two UIViewControllers called VC1 and VC2 in my demo project.
No any other additional code in this demo project. enter image description here

The right bar button in the VC1 is gray When I canceled the pop from VC2 to VC1 using pop gesture and then pop back using pop gesture or tapping the back button.
The gray style looks the same as the disabled or highlighted.

enter image description here

However the button is still enabled and the style restores when it is tapped again.
Is this a iOS12 bug?
I don't have lower device or simulators for now, I only test it on 10.0, 12.0, 12.1.
iOS 10.0 works just fine as expected.
The bug seems only happens on iOS 12 and above.
Anyone has idea about this?

1 Answers

I found two solutions to fix that:

Change system item from Custom to another one, e.g. Add.

enter image description here

enter image description here

It's a faster solution but unfortunately, you can't customize this button. To put your custom button you can try other solution:

Remove UIBarButtonItem form storyboard and then create them from code:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Test", style: .plain, target: self, action: #selector(rightBarButtonTapped))
    }

    @objc func rightBarButtonTapped() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc2 =  storyboard.instantiateViewController(withIdentifier: "viewController2")
        self.navigationController?.pushViewController(vc2, animated: true)
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Test", style: .plain, target: self, action: #selector(rightBarButtonTapped))
        super.viewWillDisappear(animated)
    }
}

Please remember set Storyboard ID for view controller to push. In my example, it's viewController2.

Edit:

You can just add UIButton instead of UIBarButtonItem and that's it ;) You can do it from storyboard level or write code.

Related