Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide or Disable UIBarButtonItem in UINavigationBar

I am presenting a UIViewController with a Modal Segue. This UIViewController inherits a UINavigationBar and I have set a UIBarButtonItem on the left side of the Navigation Bar:

HELP ME!!!

My problem is that when the viewWillAppear method of the presented UIViewController is called, I'm trying to hide the UIBarButtonItem, but the UIBarButtonItem always appears. I've declared a UIBarButtonItem for the UIViewController, named myCancelButton, and I've referenced it in Interface Builder.

Here's what I've tried so far (all of which does not work):

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.backBarButtonItem = nil;
    self.myCancelButton = nil;

    NSMutableArray *barButtonItems = [self.navigationItem.leftBarButtonItems mutableCopy];
    [barButtonItems removeObject:self.myCancelButton];
    self.navigationItem.leftBarButtonItems = barButtonItems;

    [self.navigationItem.leftBarButtonItems delete:self.myCancelButton];

    self.navigationController.navigationItem.leftBarButtonItem = nil;
    barButtonItems = [self.navigationController.navigationItem.leftBarButtonItems mutableCopy];
    [barButtonItems removeObject:self.myCancelButton];
    self.navigationController.navigationItem.leftBarButtonItems = barButtonItems;

    [self.navigationController.navigationItem.leftBarButtonItems delete:self.myCancelButton];

    [self.myCancelButton setWidth:0.01];

    self.navigationItem.hidesBackButton = TRUE;

    //Even setting enabled to false doesn't work:
    [self.myCancelButton setEnabled:FALSE];
}
like image 551
w3bshark Avatar asked Dec 13 '25 16:12

w3bshark


1 Answers

This should work.

[self.navigationItem setHidesBackButton:YES animated:YES];
like image 99
funeralfunk Avatar answered Dec 15 '25 11:12

funeralfunk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!