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'
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 ...
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.
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With