How to Hide a Bar Button Item for Certain Users

How to hide a bar button item for certain users

You can store a copy of the leftBarButtonItem in a strong property and update it after the users log in.

var leftBarButtonItem : UIBarButtonItem!

Inside viewDidLoad:

self.leftBarButtonItem = UIBarButtonItem(title: "test", style:         UIBarButtonItem.Style.Plain, target: nil, action: nil)

In logic:

if loggedIn
{
self.navigationItem.leftBarButtonItem = self.leftBarButtonItem
}
else
{
self.navigationItem.leftBarButtonItem = nil
}

How to hide bar button item

You can hide it by disable the button & change it tintColor like that,

self.navigationItem.rightBarButtonItem?.isEnabled = false
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clear

do it right or left BarButtonItem whatever you preferred. Hope it helps.

Show/Hide barButtonItem

Just got the answer! All you have to do is create a strong IBOutlet, then you can do the following:

// viewDidLoad
self.navigationItem.rightBarButtonItem = nil

// Later on...
self.navigationItem.rightBarButtonItem = self.deleteBarButton

How can I hide one bar button item on a certain view controller and show it again?

I thought that there is no count method for navigationItem.rightBarButtonItems because Xcode always marked it as red... but I only forgot a closing bracket somewhere else..

if (self.navigationItem.rightBarButtonItems?.count ?? 0) > 2 {
self.navigationItem.rightBarButtonItems?.remove(at: index)
}

seems to solves the problem for me.

Hide or remove specific item from navigation bar button items, Swift 4

Try this:

self.navigationItem.rightBarButtonItems?.remove(at: [indexOfButton])

[indexOfButton] should be the index of the button in the rightBarButtonItems array.

Hide bar button items swift

I dragged a Navigation Bar onto my View Controller

Well, don't! There is a big difference between a navigation controller interface, where you set the navigationItem, and a loosey-goosey navigation bar just sitting there in the interface, which is what you have.

Embed your view controller in a UINavigationController and do things the right way. Then setting your navigationItem and its properties will work as expected.

How do I show/hide a UIBarButtonItem?

Save your button in a strong outlet (let's call it myButton) and do this to add/remove it:

// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];

// This is how you remove the button from the toolbar and animate it
[toolbarButtons removeObject:self.myButton];
[self setToolbarItems:toolbarButtons animated:YES];

// This is how you add the button to the toolbar and animate it
if (![toolbarButtons containsObject:self.myButton]) {
// The following line adds the object to the end of the array.
// If you want to add the button somewhere else, use the `insertObject:atIndex:`
// method instead of the `addObject` method.
[toolbarButtons addObject:self.myButton];
[self setToolbarItems:toolbarButtons animated:YES];
}

Because it is stored in the outlet, you will keep a reference to it even when it isn't on the toolbar.

How can I hide or add a BarButtonItem dynamically when I change the current ViewController?

So, navigationItem.rightBarButtonItems is an array and you can remove/append elements. I'll suggest you to try removing "humburger" button item and adding the new one when DocumentationViewController is presented.

navigationItem.rightBarButtonItems?.popLast()
navigationItem.rightBarButtonItems?.append(filterBtn)


Related Topics



Leave a reply



Submit