0
votes

I currently have a custom UINavigationController written in Objective-C that has a rootViewController which implements

- (UINavigationItem *)navigationItem

The implementation looks something like this:

- (UINavigationItem *)navigationItem {
    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Map Search"];

    item.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];

    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_menu"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonPressed:)];

    item.leftBarButtonItem = leftBarButtonItem;

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Other Button" style:UIBarButtonItemStylePlain target:self action:@selector(otherButtonPressed:)];
    item.rightBarButtonItem = rightBarButtonItem;

    return item;
}

This is working correctly and displaying my buttons in the navigation bar of my rootViewController as expected. However, when I attempt to hide the bar button items, nothing happens. I've tried setting the buttons to nil and creating UIBarButtonItem strong properties to keep references to my buttons to return to previous state later, e.g.

self.navigationController.navigationItem.rightBarButtonItem = nil;
...
self.navigationController.navigationItem.rightBarButtonItem = _strongPropertyUIBarButtonReference;

I've also tried referencing the buttons via

self.navigationItem.rightBarButtonItem

I've tried hiding these buttons with the alpha property. I've tried creating a method in my custom UINavigationController class that sets the bar button item references to nil. All to no avail. Any help would be really appreciated.

2

2 Answers

0
votes

when you want to add a button on navigation Bar, you can create a UIBarButtonItem and can pass it to self.navigationItem.rightBarButtonItem. When you want to hide this bar button, you just directly make it nil. this will hide the button. self.navigationItem.rightBarButtonItem = nil;

0
votes

I finally found a solution. I had to do the following:

self.navigationController.navigationBar.topItem.leftBarButtonItem = nil;
self.navigationController.navigationBar.topItem.rightBarButtonItem = nil;

Going through the navigationBar's topItem was key. I then restore back to initial unhidden state using strong UIBarButtonItem properties.