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 a UIBarButtonItem?
UINavigationItem
doesnt have a rightBarButton
property. Try rightBarButtonItem
instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];
):
self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];
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 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
}
UIBarButtonItem hide element and space that it takes
The best solution that i found was searchButton.customView = UIView()
Make a UIBarButtonItem disappear using swift IOS
Do you really want to hide/show creeLigueBouton
? It is instead much easier to enable/disable your UIBarButtonItems. You would do this with a few lines:
if(condition == true) {
creeLigueBouton.enabled = false
} else {
creeLigueBouton.enabled = true
}
This code can even be rewritten in a shorter way:
creeLigueBouton.enabled = !creeLigueBouton.enabled
Let's see it in a UIViewController subclass:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var creeLigueBouton: UIBarButtonItem!
@IBAction func hide(sender: AnyObject) {
creeLigueBouton.enabled = !creeLigueBouton.enabled
}
}
If you really want to show/hide creeLigueBouton
, you can use the following code:
import UIKit
class ViewController: UIViewController {
var condition: Bool = true
var creeLigueBouton: UIBarButtonItem! //Don't create an IBOutlet
@IBAction func hide(sender: AnyObject) {
if(condition == true) {
navigationItem.rightBarButtonItems = []
condition = false
} else {
navigationItem.rightBarButtonItems = [creeLigueBouton]
condition = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
creeLigueBouton = UIBarButtonItem(title: "Creer", style: UIBarButtonItemStyle.Plain, target: self, action: "creerButtonMethod")
navigationItem.rightBarButtonItems = [creeLigueBouton]
}
func creerButtonMethod() {
print("Bonjour")
}
}
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.
Related Topics
Why Maskstobounds = Yes Prevents Calayer Shadow
How to Turn a Cvpixelbuffer into a Uiimage
Swift Doesn't Convert Objective-C Nserror** to Throws
Launch Apple Mail App from Within My Own App
Monotouch/Wcf: How to Consume the Wcf Service Without Svcutil
How to Put the Image on the Right Side of the Text in a Uibutton
How to Add "Done" Button to Numpad in iOS Using Swift
Custom Uitableviewcell Selection Style
Core Audio Offline Rendering Genericoutput
How to Send Data Back by Popviewcontrolleranimated for Swift
How to Fetch All Contacts Record in iOS 9 Using Contacts Framework
How to Reload Tableview from Another View Controller in Swift
How to Easily Delete All Objects in a Realm
iOS 8 - Buttons in Horizontal Scroll View Intercepting Pan Event - Scroll Does Not Work
Autolayout + Rtl + Uilabel Text Alignment
How to Handle Image Scale on All the Available iPhone Resolutions