Sreehari
Sreehari

Reputation: 201

Hiding UINavigationItem's bar button

I have added a BarButton item to the left of the nav.bar through Interface Builder and in the code I want this only to show in my table view's edit mode. But I didn't find any hidden property to set the leftBarButtonItem (like: self.navigationItem.leftBarButtonItem.hidden = YES).

I can only set enabled property. Anybody know how to control the hide and show property of the leftBarButtonItem, please help.

Upvotes: 14

Views: 31361

Answers (11)

Wasim
Wasim

Reputation: 1140

func showOrHideButton() {
        isEnabled ? showButton() : hideButton()
    }

 func showButton() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(action))
    }

    func hideButton() {
        navigationItem.leftBarButtonItem = nil
    }

Upvotes: 0

Jerome
Jerome

Reputation: 2152

I have a easy function to make this. I have a navigation like this. Navigation Bar It comes form Interface Builder, it has a background image.

@IBOutlet weak var memberBtn: UIBarButtonItem!

you can hide/show it by:

func hideMemberBtn() {
    memberBtn.isEnabled = false
    memberBtn.tintColor = UIColor.clear
}
func showMemberBtn() {
    memberBtn.isEnabled = true
    memberBtn.tintColor = UIColor.white
}

It's easy but it work for me. You can change tintColor as you needed. Hope for help :]

Upvotes: 6

Bhavsar1311
Bhavsar1311

Reputation: 773

This solution work for me

UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:myView];
self.navigationItem.leftBarButtonItem = btnL;

Upvotes: 0

Magic
Magic

Reputation: 11

Well making it nil was not a option because i wanted to show it again and didnt want to create a button again.

so what i did was

 UIBarButtonItem *barButton =  (UIBarButtonItem *)self.navBar.topItem.leftBarButtonItem;
 barButton.customView.hidden = true;//Hide
 barButton.customView.hidden = false;//Show

Works for me. (my leftBarButtonItem was created using customView)

Hope it helps.

Upvotes: 0

Ruiz
Ruiz

Reputation: 318

You can use

// Hide
self.navigationItem.leftBarButtonItem = nil;

// Show
self.navigationItem.leftBarButtonItem = self.myBarButtonItem

The key is making sure that you have a strong reference to the button item before nilling leftBarButtonItem.

@property (strong, nonatomic) IBOutlet UIBarButtonItem *myBarButtonItem;

Upvotes: 5

pkc
pkc

Reputation: 8504

To hide/disable

[self.navigationItem.leftBarButtonItem setEnabled:FALSE];

To show/enable

[self.navigationItem.leftBarButtonItem setEnabled:TRUE];

Upvotes: 1

Adrian Avendano
Adrian Avendano

Reputation: 1101

This works I tried it myself

self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;      

Upvotes: 63

C0D3
C0D3

Reputation: 6559

I just created my own "hide" function show below:

- (void)hideClearButton:(BOOL)hide {

    if (hide) {
        self.navigationItem.leftBarButtonItem = nil;
    }
    else {
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                                 initWithTitle:NSLocalizedString(@"Clear", @"Recents")
                                                 style:UIBarButtonItemStylePlain
                                                 target:self action:@selector(clearAll:)];

    }
}

You can just call it like:

[self hideClearButton:YES]; //hide it

or

[self hideClearButton:NO];  //show it

Upvotes: 3

Mohit Manhas
Mohit Manhas

Reputation: 3541

You can use

[self.navigationItem.leftBarButtonItem setEnabled:TRUE];

as there is no other way to hide it. so just disable it.

Upvotes: 1

Danilo Campos
Danilo Campos

Reputation: 6479

There's nothing in the documentation to suggest bar items have a hidden property.

Why not set

self.navigationItem.leftBarButtonItem = nil; 

when the user isn't editing, then set

self.navigationItem.leftBarButtonItem = whateverBarButtonItem; 

when the user is editing? This requires either re-creating the button each time or storing it for the duration of the view's lifecycle. Neither is terribly painful, but no, not nearly as easy as a .hidden property.

Upvotes: 2

Neil
Neil

Reputation: 1853

I'm pretty sure the only way to "hide" it is to nil it out.

self.navigationItem.leftBarButtonItem = nil;

Though it's not a perfect answer to your question, since that basically gets rid of your button instead of hiding it. You'll either have to recreate it or keep your original button around and simply set the leftBarButtonItem back to your UIBarButtonItem.

Upvotes: 9

Related Questions