navigationController?.navigationBar.isUserInteractionEnabled not working as expected

922 views Asked by At

For the following viewController hierarchy, isUserInteractionEnabled doesn't appear to be working as expected.

NavigationController(ViewController A) --- pushes to ---> NavigationController(ViewController B)

In ViewController A's viewDidAppear method I set navigationController?.navigationBar.isUserInteractionEnabled to false and set it to true in ViewController B's viewDidAppear method. However, upon popping ViewController B and returning to ViewController A, the navigation bar remains enabled for user interaction. Any thoughts as why this may be happening are greatly appreciated, thanks in advance!

1

There are 1 answers

0
staticVoidMan On

That seems to be a bug for which you could get around by doing that on the main thread:

override func viewDidAppear(_ animated: Bool) {
    //...        
    DispatchQueue.main.async {
        self.navigationController?.navigationBar.isUserInteractionEnabled = false
    }
}

But this still leaves a millisecond window where the navigationBar's interaction is enabled.
You have to be really quick.


However...

I wouldn't recommend what you're doing; i.e. disabling the navigationBar.
You could lose the back ability, if it had one, because you're just disabling the navigationBar entirely.

Suggestion:

Since every viewController in the navigation stack has it's own navigationItem, that contains it's own set of barButtonItems, I would recommend you keep references of the UIBarButtonItem and enable/disable them explicitly.

i.e.

@IBOutlet var myBarButtonItem: UIBarButtonItem!

override func viewDidAppear(_ animated: Bool) {
    //...
    myBarButtonItem.isEnabled = false
}

Furthermore, the state of this barButtonItem is handled in this viewController itself and you need not do things like self.navigationController?.navigationBar.isUserInteractionEnabled = true elsewhere.