4

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?

Indrajeet
  • 5,490
  • 2
  • 28
  • 43
JsW
  • 1,682
  • 3
  • 22
  • 34

1 Answers1

0

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.

George
  • 25,988
  • 10
  • 79
  • 133
Mateusz
  • 698
  • 1
  • 8
  • 18
  • Why are you creating `UIBarButtonItem` twice and assigning them to navigation bar right button item? – nayem Nov 01 '18 at 11:11
  • If you create only inside `viewDidLoad` it will be works like your example. Same for keeping `UIBarButtonItem` as property. Reassigned inside `viewWillDisappear` fix that gray style. – Mateusz Nov 01 '18 at 11:27
  • @Mateusz Actually, I created customized buttons in my real project. And your second solution is actually created a brand new item. I can't deny that's a workaround, but this isn't the real solution. Thanks for your work. – JsW Nov 01 '18 at 11:36
  • I tried your first solution, only those styled buttons with image on it like "add" won't turn gray. The system buttons like "Edit", "Done" are also buggy. – JsW Nov 01 '18 at 11:41
  • Replace `UIBarButtonItem` by `UIButton` ;) – Mateusz Nov 01 '18 at 12:14