How do I hide/show the right button in the Navigat

2019-01-16 11:17发布

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?

15条回答
放荡不羁爱自由
2楼-- · 2019-01-16 11:42

Show:

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

Hide:

//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
查看更多
等我变得足够好
3楼-- · 2019-01-16 11:45

SWIFT 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.

Also the following suggestion did not work for me using Xcode 7.3 and 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):

  1. 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()
    
  2. Save the items in viewDidLoad()

    sav_settingItem = nav.leftBarButtonItems![0]
    sav_logoItem = nav.leftBarButtonItems![1]
    
  3. To HIDE set them to nil

    nav.leftBarButtonItem = nil
    nav.leftBarButtonItem = nil
    
  4. To SHOW them

    if (nav.leftBarButtonItem == nil) {
        nav.leftBarButtonItem = sav_settingItem
        nav.leftBarButtonItems?.append(sav_logoItem)
        return
    }
    
查看更多
不美不萌又怎样
4楼-- · 2019-01-16 11:45

Credit has to go to learner for 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:

[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.

查看更多
太酷不给撩
5楼-- · 2019-01-16 11:46

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.

@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
查看更多
Deceive 欺骗
6楼-- · 2019-01-16 11:51

Set the the title to empty first and after swlwction just set again.

查看更多
贼婆χ
7楼-- · 2019-01-16 11:55

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.

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:

//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;
查看更多
登录 后发表回答