how to disable a navigation bar button item in iOS

2020-03-01 03:25发布

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 ?

16条回答
混吃等死
2楼-- · 2020-03-01 03:28

For version iOS 10.3, swift 3:

self.navigationItem.rightBarButtonItem?.isEnabled = false.
查看更多
Rolldiameter
3楼-- · 2020-03-01 03:30

Swift 5 & iOS 13 :

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

        self.navigationItem.leftBarButtonItems = []
查看更多
太酷不给撩
4楼-- · 2020-03-01 03:31
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

查看更多
干净又极端
5楼-- · 2020-03-01 03:33

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...!

查看更多
beautiful°
6楼-- · 2020-03-01 03:34

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;
查看更多
冷血范
7楼-- · 2020-03-01 03:36

You can do the following if you are running on Swift

self.navigationItem.rightBarButtonItem?.enabled = true

This snippet will disable the button.

查看更多
登录 后发表回答