Sravan
Sravan

Reputation: 1941

how to disable a navigation bar button item in iOS

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 ?

Upvotes: 35

Views: 48464

Answers (16)

Nheng Vanchhay
Nheng Vanchhay

Reputation: 21

Swift 5

self.navigationItem.rightBarButtonItem?.isEnabled = true;

self.navigationItem.rightBarButtonItem?.isEnabled = false;

Upvotes: 0

Ghulam Rasool
Ghulam Rasool

Reputation: 4064

One line solution

self.navigationItem.leftBarButtonItem.enabled = NO;

self.navigationItem.rightBarButtonItem.enabled = NO;

Upvotes: 1

Fadi
Fadi

Reputation: 743

Swift 5 & iOS 13 :

To remove all left buttons or just a specified one just remove from leftBarButtonItems Array.

        self.navigationItem.leftBarButtonItems = []

Upvotes: 0

andrewlundy_
andrewlundy_

Reputation: 1143

The simplest way to truly disable a UIBarButtonItem would be as followed:

barButtonVar.isEnabled = false

Upvotes: 2

Kavin Varnan
Kavin Varnan

Reputation: 1989

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

Upvotes: 3

Diviyo
Diviyo

Reputation: 57

Navigation bar button items must be toggled by referring to them via the navigationItem property.

For example:

func setupNav() {
    let saveButton = UIBarButtonItem.init(barButtonSystemItem: .save, target: self, action: #selector(onSavePressed))
    navigationItem.rightBarButtonItem = saveButton
    saveButton.isEnabled = false
  }

func validateSave() {
     saveButton.isEnabled = isConditionMet // WON'T work
      navigationItem.rightBarButtonItem.isEnabled = isConditionMet // WORKS!
}

Upvotes: -1

Komicon
Komicon

Reputation: 11

var menuBtn = new UIButton(UIButtonType.Custom);
menuBtn.Frame = new CGRect(x: 0.0, y: 0.0, width: 20, height: 20);
menuBtn.SetImage(new UIImage("filter"), UIControlState.Normal);

menuBtn.Alpha = 0.05f;   //to set the Alpha

menuBtn.Enabled = false;

tested on Mvvmcross Xamarin.iOS only

Upvotes: -2

Muhammad Fasi
Muhammad Fasi

Reputation: 107

Try this code:

UIApplication.sharedApplication().beginIgnoringInteractionEvents()

This will stop user to interaction with app and after service call, write this code again:

UIApplication.sharedApplication().endIgnoringInteractionEvents()

Sure this will help.

Upvotes: 2

Sagar D
Sagar D

Reputation: 223

For version iOS 10.3, swift 3:

self.navigationItem.rightBarButtonItem?.isEnabled = false.

Upvotes: 2

Kiran Jadhav
Kiran Jadhav

Reputation: 3317

Updated for Swift 3:

If you want to disable a navigation bar button item OR you want to disable hole UINavigationBar i.e. all item present on navigation bar, use below lines of code;

// if you want disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false

// if you want enable again
self.navigationController?.navigationBar.isUserInteractionEnabled = true

Enjoy...!

Upvotes: 3

Sjakelien
Sjakelien

Reputation: 2317

I solved this by just adding a property to my viewcontroller:

@property (nonatomic, strong) IBOutlet UIBarButtonItem * RightButton;

I then connected it to the button on the storyboard. You can then at will set its properties like:

self.RightButton.enabled=true;

Upvotes: 1

user4919266
user4919266

Reputation:

- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
}

Upvotes: 3

ObjectiveTC
ObjectiveTC

Reputation: 2507

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

Upvotes: 7

Jaya Mayu
Jaya Mayu

Reputation: 17247

You can do the following if you are running on Swift

self.navigationItem.rightBarButtonItem?.enabled = true

This snippet will disable the button.

Upvotes: 16

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;

Upvotes: 59

jbouaziz
jbouaziz

Reputation: 1493

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;

Upvotes: 9

Related Questions