Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/Hide barButtonItem [duplicate]

I'm trying to show/hide a UIBarButtonItem. I added a barButton to the right side in the storyboard. Then in viewDidLoad, I made the rightBarButtonItem to nil. Later on, I set it to the button I added in the storyboard. Here's my code:

// Right barButtonItem added in storybord: @IBOutlet weak var deleteBarButton: UIBarButtonItem!   // viewDidLoad self.navigationItem.rightBarButtonItem = nil  // Later on... self.navigationItem.rightBarButtonItem = self.deleteBarButton 

When I set self.deleteBarButton to the rightBarButtonItem, nothing happens. It doesn't show it. What am I doing wrong, and what's the correct/most efficient way to show/hide a barButtonItem?

Update

I tried the following:

self.deleteBarButton.hidden = true 

But I get the following error:

UIBarButtonItem does not have a member named 'hidden'

like image 545
Jessica Avatar asked Sep 13 '15 18:09

Jessica


People also ask

How do I hide Barbuttonitem?

UIBarButtonItem doesn't have a hidden property, and any examples I've found so far for hiding them involve setting nav bar buttons to nil, which I don't think I want to do here because I may need to show the button again (not to mention that, if I connect my button to an IBOutlet, if I set that to nil I'm not sure how ...

How do I hide Navigationitem?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


2 Answers

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 
like image 144
Jessica Avatar answered Sep 19 '22 23:09

Jessica


Update 2

You could just set the button's text to nothing:

self.deleteBarButton.title = ""; 

Update 1

I would use the enabled property to illuminate the button as follows (although it does not completely make the button invisible, it allows the user to know it will not perform an action).

This can act as a variable to let you know that the button is hidden in your case:

Illuminated: (place in ViewDidLoad)

self.deleteBarButton.enabled = true; 

Darkened: (place later on)

self.deleteBarButton.enabled = false; 

Then I would add the following to make it completely disappear:

self.navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.clearColor(); 
like image 24
Jake Chasan Avatar answered Sep 20 '22 23:09

Jake Chasan