How do I hide/show the right button in the Navigation Bar

前端 未结 18 1940
后悔当初
后悔当初 2020-12-12 19:25

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:



        
相关标签:
18条回答
  • 2020-12-12 19:52

    My solution:

    self.navigationItem.rightBarButtonItem.customView.hidden=NO;
    
    0 讨论(0)
  • 2020-12-12 19:52

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

    0 讨论(0)
  • 2020-12-12 19:54

    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
    
    0 讨论(0)
  • 2020-12-12 19:56

    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;
    
    0 讨论(0)
  • 2020-12-12 19:56

    Set reference to 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.

    0 讨论(0)
  • 2020-12-12 19:57

    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.

    0 讨论(0)
提交回复
热议问题