Search code examples
iphoneuibarbuttonitemuinavigationitem

Remove a BarButtonItem from NavigationItem


I am creating an app in which I am using navigation Controller. I had done with adding and removing UIBarButtonItem from the navigation Bar. I have back button at my left side and a additional button at right side named (MORE).

Now my requirement is when I click on the MORE I need to add a CLOSE button on the left side of Navigation Bar and Back button should be hide. I am done with this too.

The problem is while removing the CLOSE button I am using like:

self.navigationItem.leftBarButtonItem = nil;

It removes my back button too. I need to keep back button and only want to remove the button.

I don't know whether I am right and I need to write code for displaying back button back. or is there any way by which I can remove only CLOSE button on the click of MORE button or CLOSE itself.

Thanks


Solution

  • If you tap on MORE button, then BACK button should be hide and MORE should be added on left side of NavBar so you can do this as:

    -(void) moreButtonClicked{
    
    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"CLOSE" style:UIBarButtonItemStyleBordered target:self action:@selector (closeButtonClicked:)];
    self.navigationItem.leftBarButtonItem = closeButton;
    [closeButton release];
    
    }
    

    when you want to remove your CLOSE button and set your BACK button at previous place then try this as:

    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"BACK" style:UIBarButtonItemStyleBordered target:self action:@selector (backButtonClicked:)];
    self.navigationItem.leftBarButtonItem = backButton;
    [backButton release];