ios 如何隐藏/显示导航栏中的右侧按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5588767/
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 do I hide/show the right button in the Navigation Bar
提问by Lauren Quantrell
I need to hide the right button in the Navigation Bar, then unhide it after the user selects some options.
我需要隐藏导航栏中的右键,然后在用户选择一些选项后取消隐藏它。
Unfortunately, the following doesn't work:
不幸的是,以下方法不起作用:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
Is there a way?
有办法吗?
回答by Matt J
Hide the button by setting the reference to nil, however if you want to restore it later, you'll need to hang onto a copy of it so you can reassign it.
通过将引用设置为 nil 来隐藏按钮,但是如果您想稍后恢复它,则需要保留它的副本,以便您可以重新分配它。
UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;
//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];
Personally, in my apps I make my nav buttons into @properties, so that I can trash & recreate them at will, so something like:
就个人而言,在我的应用程序中,我将导航按钮设置为 @properties,以便我可以随意删除和重新创建它们,例如:
//mycontroller.h
UIBarButtonItem *rightNavButton;
@property (nonatomic, retain) UIBarButtonItem *rightNavButton;
//mycontroller.m
@synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
if (!rightNavButton) {
rightNavButton = [[UIBarButtonItem alloc] init];
//configure the button here
}
return rightNavButton;
}
//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;
回答by richc
For Swift 3
对于 Swift 3
if let button = self.navigationItem.rightBarButtonItem {
button.isEnabled = false
button.tintColor = UIColor.clear
}`
回答by Stefan Ticu
Set reference to nil:
将引用设置为 nil:
current_controller_in_navcontroller.navigationItem.rightBarButtonItem = nil;
Also be sure to call this in the controller currently shown by the navController, not for the navController itself.
还要确保在 navController 当前显示的控制器中调用它,而不是在 navController 本身。
回答by HugoMasterPL
Show:
展示:
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
Hide:
隐藏:
[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];
You can even animate its showing/hiding
您甚至可以为其显示/隐藏设置动画
[UIView animateWithDuration:0.2 animations:^{
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
}];
回答by mmackh
Here's Matt's solution updated for Storyboards & ARC. Remember, IBOutlets are __weak by default, so you need to change that to strong for it not to be released too early.
这是 Matt 为 Storyboards & ARC 更新的解决方案。请记住,IBOutlets 默认为 __weak,因此您需要将其更改为 strong 以免过早发布。
@interface MAGTableViewController () <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIBarButtonItem *rightBarButton;
@end
@implementation MAGTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setRightBarButtonItem:nil];
}
- (IBAction)rightBarButtonItemTapped:(id)sender
{
[self.view endEditing:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.navigationItem setRightBarButtonItem:self.rightBarButton];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self.navigationItem setRightBarButtonItem:nil];
}
@end
回答by App Dev Guy
Credit has to go to learnerfor this answer which the answer is from this question:
这个答案必须归功于学习者,答案来自这个问题:
hide and show left navigation bar button on demand in iOS-7
This is the answer, which is far more simple.
这就是答案,它要简单得多。
//hide and reveal bar buttons
-(void) hideAndDisableLeftNavigationItem
{
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor clearColor]];
[self.navigationItem.leftBarButtonItem setEnabled:NO];
}
-(void) showAndEnableLeftNavigationItem
{
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor blueColor]];
[self.navigationItem.leftBarButtonItem setEnabled:YES];
}
Then you just reference the method where you require it like within an (IBAction)
like so:
然后你只需在你需要的地方引用方法,(IBAction)
就像这样:
[self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again
I tried all other methods and none worked, even referencing my button as a @property (...) UIBarButtonItem....
and nothing worked until I found this.
我尝试了所有其他方法,但都没有奏效,甚至将我的按钮称为 a@property (...) UIBarButtonItem....
并且在我找到它之前没有任何效果。
回答by Zvi
SWIFT 2.2
斯威夫特 2.2
In swift 2.2 self.navigationItem does not work. Instead create an outlet of the NavigationItem (I named it below "nav") and use it.
在 swift 2.2 self.navigationItem 中不起作用。而是创建 NavigationItem 的出口(我将其命名为“nav”)并使用它。
Also the following suggestion did not work for me using Xcode 7.3 and swift 2.2
此外,以下建议对我使用 Xcode 7.3 和 swift 2.2 不起作用
nav.leftBarButtonItem?.customView?.hidden = true
So I used the idea of @Matt J above as follows (I have 2 items on the left):
所以我使用了上面@Matt J的想法如下(我左边有2个项目):
Create outlets for the items in the navigation bar and variables to store them
@IBOutlet weak var settingItem: UIBarButtonItem! @IBOutlet weak var logoItem: UIBarButtonItem! var sav_settingItem: UIBarButtonItem = UIBarButtonItem() var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
Save the items in viewDidLoad()
sav_settingItem = nav.leftBarButtonItems![0] sav_logoItem = nav.leftBarButtonItems![1]
To HIDE set them to nil
nav.leftBarButtonItem = nil nav.leftBarButtonItem = nil
To SHOW them
if (nav.leftBarButtonItem == nil) { nav.leftBarButtonItem = sav_settingItem nav.leftBarButtonItems?.append(sav_logoItem) return }
为导航栏中的项目和变量创建出口以存储它们
@IBOutlet weak var settingItem: UIBarButtonItem! @IBOutlet weak var logoItem: UIBarButtonItem! var sav_settingItem: UIBarButtonItem = UIBarButtonItem() var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
在 viewDidLoad() 中保存项目
sav_settingItem = nav.leftBarButtonItems![0] sav_logoItem = nav.leftBarButtonItems![1]
隐藏设置它们为零
nav.leftBarButtonItem = nil nav.leftBarButtonItem = nil
给他们看
if (nav.leftBarButtonItem == nil) { nav.leftBarButtonItem = sav_settingItem nav.leftBarButtonItems?.append(sav_logoItem) return }
回答by Amr Angry
Show:
展示:
[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
Hide:
隐藏:
[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
回答by kozla13
My solution:
我的解决方案:
self.navigationItem.rightBarButtonItem.customView.hidden=NO;
回答by Vahid
Swift 2:
斯威夫特2:
Trick!
诡计!
Hide:
隐藏:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = false
btn.title = ""
}
Show:
展示:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = true
btn.title = "ButtonName"
}