1

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!

5
  • by "pushing" you mean present modally? I think you can't "push" another navigation controller on an existing navigation controller, correct? Commented Apr 12, 2018 at 4:42
  • Or do you have one single navigation controller and A is root and you push B on it? Commented Apr 12, 2018 at 4:42
  • Did you set a break point inside A's viewDidAppear to see if it was executed? Commented Apr 12, 2018 at 4:43
  • try set it false inside viewWillAppear instead viewDidAppear. Commented Apr 12, 2018 at 4:59
  • Upload code so we know what you doing Commented Apr 12, 2018 at 5:01

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.