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.
Trying to make rightBarButton appear after making it disappear using Swift
Once you set the bar button item to nil
, it is gone. Something you can do however, is store the bar button item like so:
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(TableViewController.dismiss));
and then you can make it appear/disappear like so:
self.navigationItem.rightBarButtonItem = barButtonItem
self.navigationItem.setRightBarButtonItem(nil, animated: true)
then just access the barButtonItem
whenever you want it to appear/disappear.
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];
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.
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.
Bar Button Items disappear from Navigation Controller
Opps ... turns out I was to blame for the disappearing bar button items.
I was loading a SKStoreProductViewController (presents the App Store view controller) and I wanted to make the colors match my app so after a search on the web I found a solution:
UINavigationBar.appearance().tintColor = myColor
And it worked.
But this did not affect my apps Bar Button Items. It wasn't until I called MFMailComposeViewController (to compose an email) that the problem occurred.
When closing MFMailComposeViewController my apps Bar Button Items disappeared.
Well they didn't disappear, they turned the same color as the Navigation Bar, they were still there, just couldn't see them.
The solution was to set the tint color of the SKStoreProductViewController like this:
myStoreProductViewController.view.tintColor = myColor
UIToolbarItems and UIBarButtonItems not appearing in simulator or getting cut off, am I missing something?
You need to embedded your ViewController
in a Navigation Controller.
Open your storyboard -> Editor -> Embed in -> Navigation Controller.
Also, your code is missing a curly brace after navigationController?.isToolbarHidden = false
to close the viewDidLoad
method.
Related Topics
How to Simulate Mouse Click from MAC App to Other Application
Programmatically Creating Constraints Bound to View Controller Margins
Nsapplicationdelegate Not Working Without Storyboard
Process Array in Parallel Using Gcd
How Set Rootviewcontroller in Scene Delegate iOS 13
Grab Frames from Video Using Swift
Pass C Function Callback in Swift
How to Capture Depth Data from Camera in iOS 11 and Swift 4
Checking If Textfields Are Empty Swift
Swift Update Label (With HTML Content) Takes 1Min
Back Button Image - What Is It Called in Swift
Does Swift Guarantee the Storage Order of Fields in Classes and Structs
How Do People Deal with Iterating a Swift Struct Value-Type Property
Two Tables on One View in Swift
Does Swift Have a Null Coalescing Operator and If Not, What Is an Example of a Custom Operator
Checking If an Array of Custom Objects Contain a Specific Custom Object