how to disable a navigation bar button item in iOS

44,337

Solution 1

Try this

Recommended

self.navigationItem.leftBarButtonItem.enabled = NO;

self.navigationItem.rightBarButtonItem.enabled = NO;

Or Simply Disable by

on Edge case

self.view.window.userInteractionEnabled = NO;

Update: Recently Apple doesn't allow the back button to enable / disable. Instead of that we can hide it.

self.navigationItem.hidesBackButton = YES;

Solution 2

You can do the following if you are running on Swift

self.navigationItem.rightBarButtonItem?.enabled = true

This snippet will disable the button.

Solution 3

Just disable your UINavigationController view and navigation bar interaction:

self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.view.userInteractionEnabled = NO;

And enable it when you need it back:

self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.view.userInteractionEnabled = YES;

Solution 4

Latest Swift: To hide the back button, you MUST use:

self.navigationItem.setHidesBackButton(true, animated: false)

Note: This can trigger a bug in the navigation bar that can cause an artifact to appear in place of a hidden back button when transitioning to a view that doesn't have a back button (or has a leftButton in its place). The artifact that appears is either ellipses "..." or the title of the previous viewController on the stack. I believe this bug to be related to the bug documented in apple's own sample code project "CustomizingUINavigationBar", CustomBackButtonViewController.m

Solution 5

This code should work on Swift 4.2

self.navigationItem.rightBarButtonItem?.isEnabled = false

The above code will disable the button. To enable it switch the boolean to true

Share:
44,337
Sravan
Author by

Sravan

Updated on September 20, 2021

Comments

  • Sravan
    Sravan over 2 years

    I have created a navigation controller. In the second view (which is pushed), I have some webservice call and placing a overlay view and setting

    self.view.userInteractionEnabled = NO ;

    Once web service call is complete, then I am reverting to

    self.view.userInteractionEnabled = YES ;

    When I do this, every other buttons except the buttons on the navigation bar are disabled. How to disable those two navigation bar button items ? (a button similar to back button, which pops to first view controller and another button which gives info about help).

    I have tried using self.navigationItem.backBarButtonItem.enabled = NO. But still I am able to tap on the button and can navigate to first screen. How can I disable these two buttons ?