47

我需要隐藏导航栏中的右键,然后在用户选择一些选项后取消隐藏。

不幸的是,以下方法不起作用:

NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES;  // FOO CODE

有办法吗?

4

18 回答 18

80

通过将引用设置为 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;
于 2011-04-07T23:57:03.283 回答
17

对于斯威夫特 3

if let button = self.navigationItem.rightBarButtonItem {
                    button.isEnabled = false
                    button.tintColor = UIColor.clear
                }`
于 2017-03-23T12:03:55.213 回答
11

显示:

[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];

    }];
于 2013-10-15T07:40:13.010 回答
10

将引用设置为 nil:

current_controller_in_navcontroller.navigationItem.rightBarButtonItem =  nil;

还要确保在 navController 当前显示的控制器中调用它,而不是为 navController 本身调用。

于 2011-04-07T23:44:04.570 回答
5

这是 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
于 2013-07-21T08:52:41.333 回答
5

如果右侧只有一个条形按钮项,则可以使用此项,

self.navigationItem.rightBarButtonItem = nil;

假设您在右侧有多个条形按钮,例如假设您在导航项的右侧有两个条形按钮项(搜索按钮和过滤按钮)。现在右栏按钮项目是

self.navigationItem.rightBarButtonItems = [searchItem,filterItem]

你必须只隐藏搜索按钮,你可以使用喜欢,

self.navigationItem.rightBarButtonItems = [filterItem]

现在发生的事情是,您可以从导航项中完全隐藏搜索按钮,并且过滤器项代替搜索项

于 2019-02-20T06:44:30.030 回答
4

这个答案必须归功于学习者,答案来自这个问题:

在 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

我尝试了所有其他方法,但都没有奏效,甚至将我的按钮引用为 a@property (...) UIBarButtonItem....并且在我找到它之前没有任何效果。

于 2014-12-03T02:57:33.630 回答
4

斯威夫特 2.2

在 swift 2.2 self.navigationItem 中不起作用。而是创建 NavigationItem 的出口(我将其命名为“nav”下方)并使用它。

此外,以下建议对使用 Xcode 7.3 和 swift 2.2 的我也不起作用

 nav.leftBarButtonItem?.customView?.hidden = true

所以我使用了上面@Matt J的想法如下(我左边有2个项目):

  1. 为导航栏中的项目和变量创建出口以存储它们

    @IBOutlet weak var settingItem: UIBarButtonItem!
    @IBOutlet weak var logoItem: UIBarButtonItem!
    
    var sav_settingItem: UIBarButtonItem = UIBarButtonItem()
    var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
    
  2. 将项目保存在 viewDidLoad()

    sav_settingItem = nav.leftBarButtonItems![0]
    sav_logoItem = nav.leftBarButtonItems![1]
    
  3. 隐藏设置它们为零

    nav.leftBarButtonItem = nil
    nav.leftBarButtonItem = nil
    
  4. 向他们展示

    if (nav.leftBarButtonItem == nil) {
        nav.leftBarButtonItem = sav_settingItem
        nav.leftBarButtonItems?.append(sav_logoItem)
        return
    }
    
于 2016-09-02T14:28:18.597 回答
4

显示:

[self.navigationItem.rightBarButtonItem.customView setHidden:NO];

隐藏:

[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
于 2017-03-05T19:21:15.327 回答
3

我的解决方案:

self.navigationItem.rightBarButtonItem.customView.hidden=NO;
于 2015-10-28T12:08:10.253 回答
2

斯威夫特 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"
}
于 2017-03-15T11:18:55.077 回答
1

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
}

第 4 步:只需调用您想要的功能,hideNavigationButton()用于隐藏和showNavigationButton()显示按钮。

问候!

于 2018-10-27T19:36:14.397 回答
1

您可以使用以下代码:

    self.navigationItem.rightBarButtonItem?.image = nil
    self.navigationItem.rightBarButtonItem?.isEnabled = false
于 2019-05-18T11:18:14.127 回答
1

对于 swift 5 隐藏 rightBarButtonItem

self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
于 2019-08-01T06:14:31.690 回答
0

显示:

//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

隐藏:

//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
于 2014-09-19T07:34:33.263 回答
0

隐藏:

if let topItem = self.navigationController?.navigationBar.topItem {
    topItem.rightBarButtonItem = nil
}
于 2017-02-23T14:38:16.340 回答
0
  1. 假设您可以将特定的栏按钮引用为变量xxxButton

(请打开助手编辑器,Control+Drag xxx 按钮到 YourViewController 类作为插座“xxxButton”)。

或者你可以使用类似的东西let xxxButton = navigationBar.buttons[1]

  1. 隐藏 xxxButton.customView = UIView() navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)

  2. 显示 xxxButton.customView = nil navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)

希望有帮助。

于 2017-02-28T13:20:29.443 回答
0

首先将标题设置为空,然后在 swlwction 之后再次设置。

于 2018-10-27T19:56:13.740 回答