【发布时间】:2011-04-07 23:22:44
【问题描述】:
我需要隐藏导航栏中的右键,然后在用户选择一些选项后取消隐藏。
很遗憾,以下方法不起作用:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有办法吗?
【问题讨论】:
标签: ios objective-c uibarbuttonitem rightbarbuttonitem
我需要隐藏导航栏中的右键,然后在用户选择一些选项后取消隐藏。
很遗憾,以下方法不起作用:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有办法吗?
【问题讨论】:
标签: ios objective-c uibarbuttonitem rightbarbuttonitem
通过将引用设置为 nil 来隐藏按钮,但是如果您想稍后恢复它,您需要保留它的副本以便重新分配它。
UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;
//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];
就个人而言,在我的应用程序中,我将导航按钮设置为 @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;
【讨论】:
对于 Swift 3
if let button = self.navigationItem.rightBarButtonItem {
button.isEnabled = false
button.tintColor = UIColor.clear
}`
【讨论】:
显示:
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
隐藏:
[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];
您甚至可以为其显示/隐藏设置动画
[UIView animateWithDuration:0.2 animations:^{
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
}];
【讨论】:
设置对 nil 的引用:
current_controller_in_navcontroller.navigationItem.rightBarButtonItem = nil;
还要确保在 navController 当前显示的控制器中调用它,而不是为 navController 本身调用。
【讨论】:
这是 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
【讨论】:
如果右侧只有一个条形按钮项,可以使用这个,
self.navigationItem.rightBarButtonItem = nil;
假设您在右侧有多个条形按钮,例如假设您在导航项的右侧有两个条形按钮项(搜索按钮和过滤按钮)。现在右栏按钮项目是
self.navigationItem.rightBarButtonItems = [searchItem,filterItem]
你只需要隐藏搜索按钮,你可以使用like,
self.navigationItem.rightBarButtonItems = [filterItem]
现在发生的事情是,您可以从导航项中完全隐藏搜索按钮,过滤器项代替搜索项
【讨论】:
这个答案的答案必须去 learner,答案来自这个问题:
hide and show left navigation bar button on demand in iOS-7
这就是答案,要简单得多。
//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];
}
然后您只需在 (IBAction) 中引用您需要的方法,如下所示:
[self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again
我尝试了所有其他方法,但都没有奏效,甚至将我的按钮引用为@property (...) UIBarButtonItem....,但在我找到它之前没有任何效果。
【讨论】:
SWIFT 2.2
在 swift 2.2 self.navigationItem 中不起作用。而是创建 NavigationItem 的出口(我将其命名为“nav”)并使用它。
以下建议也不适用于我使用 Xcode 7.3 和 swift 2.2
nav.leftBarButtonItem?.customView?.hidden = true
所以我使用了上面@Matt J的思路如下(我左边有2个项目):
为导航栏中的项目和变量创建出口
@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
}
【讨论】:
显示:
[self.navigationItem.rightBarButtonItem.customView setHidden:NO];
隐藏:
[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
【讨论】:
我的解决方案:
self.navigationItem.rightBarButtonItem.customView.hidden=NO;
【讨论】:
斯威夫特 2:
诡计!
隐藏:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = false
btn.title = ""
}
显示:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = true
btn.title = "ButtonName"
}
【讨论】:
在 swift 4 我有一个技巧来显示/隐藏左右按钮:
第 1 步: 在视图控制器中创建一个 IBOutlet 按钮:
@IBOutlet var navigationItemButton: UIBarButtonItem!
第二步:创建隐藏按钮功能:
func hideNavigationButton() {
navigationItemButton.isEnabled = false
navigationItemButton.tintColor = UIColor.clear
}
第三步:创建显示按钮功能:
func showNavigationButton() {
navigationItemButton.isEnabled = true
navigationItemButton.tintColor = UIColor.white
}
第四步:调用你想要的函数,hideNavigationButton()隐藏,showNavigationButton()显示按钮。
问候!
【讨论】:
您可以使用以下代码:
self.navigationItem.rightBarButtonItem?.image = nil
self.navigationItem.rightBarButtonItem?.isEnabled = false
【讨论】:
为 swift 5 隐藏 rightBarButtonItem
self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
【讨论】:
显示:
//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
隐藏:
//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
【讨论】:
xxxButton
(请打开助手编辑器,Control+Drag xxx 按钮到 YourViewController 类作为出口“xxxButton”)。
或者你可以使用类似let xxxButton = navigationBar.buttons[1]
隐藏
xxxButton.customView = UIView()
要么
navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)
显示
xxxButton.customView = nil
要么
navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)
希望有帮助。
【讨论】:
隐藏:
if let topItem = self.navigationController?.navigationBar.topItem {
topItem.rightBarButtonItem = nil
}
【讨论】:
先设置标题为空,然后再设置swlwction。
【讨论】: