如何在 iOS 中禁用导航栏按钮项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25362050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to disable a navigation bar button item in iOS
提问by Sravan
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 ;
self.view.userInteractionEnabled = NO ;
Once web service call is complete, then I am reverting to
Web 服务调用完成后,我将恢复到
self.view.userInteractionEnabled = YES ;
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 ?
我试过使用self.navigationItem.backBarButtonItem.enabled = NO
. 但我仍然可以点击按钮并导航到第一个屏幕。如何禁用这两个按钮?
回答by Vijay-Apple-Dev.blogspot.com
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;
回答by Jay Mayu
You can do the following if you are running on Swift
如果您正在运行,您可以执行以下操作 Swift
self.navigationItem.rightBarButtonItem?.enabled = true
This snippet will disable the button.
此代码段将禁用该按钮。
回答by jbouaziz
Just disable your UINavigationController
view and navigation bar interaction:
只需禁用您的UINavigationController
视图和导航栏交互:
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;
回答by ObjectiveTC
Latest Swift: To hide the back button, you MUST use:
最新 Swift:要隐藏后退按钮,您必须使用:
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
注意:这可能会触发导航栏中的错误,当转换到没有后退按钮(或在其位置有左按钮)的视图时,可能会导致出现一个工件来代替隐藏的后退按钮。出现的工件要么是省略号“...”,要么是堆栈上前一个 viewController 的标题。我相信这个错误与苹果自己的示例代码项目“CustomizingUINavigationBar”中记录的错误有关,CustomBackButtonViewController.m
回答by Kavin Varnan
This code should work on Swift 4.2
此代码应该适用于 Swift 4.2
self.navigationItem.rightBarButtonItem?.isEnabled = false
The above code will disable the button. To enable it switch the boolean to true
上面的代码将禁用按钮。要启用它,将布尔值切换为 true
回答by andrewlundy
The simplest way to truly disable a UIBarButtonItem would be as followed:
真正禁用 UIBarButtonItem 的最简单方法如下:
barButtonVar.isEnabled = false
回答by andrewlundy
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
}
回答by Muhammad Fasi
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.
当然这会有所帮助。
回答by Sjakelien
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;
回答by Sagar D
For version iOS 10.3, swift 3:
对于 iOS 10.3 版本,swift 3:
self.navigationItem.rightBarButtonItem?.isEnabled = false.