可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 ?
回答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;
回答2:
You can do the following if you are running on Swift
self.navigationItem.rightBarButtonItem?.enabled = true
This snippet will disable the button.
回答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;
回答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
回答5:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
}
回答6:
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
回答7:
The simplest way to truly disable a UIBarButtonItem would be as followed:
barButtonVar.isEnabled = false
回答8:
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;
回答9:
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...!
回答10:
For version iOS 10.3, swift 3:
self.navigationItem.rightBarButtonItem?.isEnabled = false.
回答11:
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.
回答12:
One line solution
self.navigationController?.view.isUserInteractionEnabled = false
回答13:
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
回答14:
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!
}
回答15:
Swift 5 & iOS 13 :
To remove all left buttons or just a specified one just remove from leftBarButtonItems
Array.
self.navigationItem.leftBarButtonItems = []
回答16:
Swift 5
It's working for Navigation controller
//Disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false
//Enable
self.navigationController?.navigationBar.isUserInteractionEnabled = true