I have an IBOutlet that I have linked to from the storyboard
@IBOutlet var creeLigueBouton: UIBarButtonItem!
and I want to make it disappea
Use the property enabled and tintColor
let barButtonItem:UIBarButtonItem? = nil
if isHidden{
barButtonItem?.enabled = false
barButtonItem?.tintColor = UIColor.clearColor()
}else{
barButtonItem?.enabled = true
barButtonItem?.tintColor = nil
}
I have more that 2 menuitems and remove/add menuitem is an overhead. This code snippet worked for me (Using Swift3).
func showMenuItem(){
menuItemQuit.customView?.isHidden = false
menuItemQuit.plainView.isHidden = false
}
func hideMenuItem(){
menuItemQuit.customView?.isHidden = true
menuItemQuit.plainView.isHidden = true
}
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")
}
}
// Nice answer haiLong, I think as an extension this is more convenient.
extension UIBarButtonItem {
var isHidden: Bool {
get {
return !isEnabled && tintColor == .clear
}
set {
tintColor = newValue ? .clear : nil
isEnabled = !newValue
}
}
}
EDIT: Removed forced unwrapping and fixed enabled value.
It is quite late to reply but looking for an answer for my problem I found this topic. The marked answer did not help me, but I managed to solve my problem thanks to @haiLong's answer. My solution works for all types of bar buttons... I think. Add this to your ViewController and use as needed.
var tintColorsOfBarButtons = [UIBarButtonItem: UIColor]()
func hideUIBarButtonItem(button: UIBarButtonItem) {
if button.tintColor != UIColor.clear {
tintColorsOfBarButtons[button] = button.tintColor
button.tintColor = UIColor.clear
button.isEnabled = false
}
}
func showUIBarButtonItem(button: UIBarButtonItem) {
if tintColorsOfBarButtons[button] != nil {
button.tintColor = tintColorsOfBarButtons[button]
}
button.isEnabled = true
}
I hope it saves some time to other developers :)
Try this. (Make newbackbutton global variable)
override func viewDidLoad() {
let newBackButton = UIBarButtonItem()
newBackButton.title = " << Return to Gallery"
newBackButton.style = UIBarButtonItemStyle.Done
newBackButton.target = self
newBackButton.action = "backtoScoutDetail:"
self.navigationItem.rightBarButtonItem = newBackButton
}
override func viewWillAppear(animated: Bool) {
newBackButton.title = ""
self.navigationItem.rightBarButtonItem = newBackButton
}