grabury
grabury

Reputation: 5559

How to hide a bar button item for certain users

I have a settings bar button item (set as left bar button item). I only want to display it if the user is logged in.

I thought I could use the following for anonymous users

navigationItem.leftBarButtonItem = nil

But then how would I show it as soon as they logged in?

Upvotes: 20

Views: 28870

Answers (7)

RAHUL NIMJE
RAHUL NIMJE

Reputation: 91

Swift 5

A better solution and works even if you have set a custom navigation bar. Hide navigation bar button item or back button leftBarButtonItem / rightBarButtonItem

    if login == true {       
    self.navigationItem.leftBarButtonItem = nil     
    } else { 
      print("set your bar button or return")
    }

Hide back bar button in navigation controller with swift 5

self.navigationItem.leftBarButtonItem = nil

self.navigationItem.hidesBackButton = true

Upvotes: 0

dobiho
dobiho

Reputation: 1035

I have a same problem and solved. I have a bar button item with image

barbtnClose.isEnabled = false
barbtnClose.image = nil

barbtnClose.customView?.isHidden = true // do not work in iOS 13

Upvotes: 1

zeeshan
zeeshan

Reputation: 5053

Swift 5.x

I faced the same dilemma and unfortunately no solution worked for me. Adding and removing buttons and related segues is unnecessarily too much of code when it includes multiple buttons on multiple screens. I have taken this approach for one or two buttons in the past and it becomes pretty ugly pretty fast.

The code menuItemQuit.customView?.isHidden = false doesn't seem to work on iOS 13 and above either, otherwise it would have made life so much easier.

My approach was to simply disable the bar button and change its tint to the navigation colour's tint.

In my app What.To.Eat I display bar buttons based on user's login status. Every element of the app is themed so that I could control all the colors based on various factors.

The navigation bar's color is named commonButtonColor and the bar buttons tint color is named commonButtonColor.

When I have to hide a bar button, I simply do the following:

let nav = self.navigationController?.navigationBar
nav?.tintColor = Theme.shared.titleText
nav?.barTintColor = Theme.shared.headerBg

if person.loggedIn {
    mealPrefsBarButton.tintColor = Theme.shared.commonButtonColor
    mealPrefsButton.isEnabled = true
} else {
    mealPrefsBarButton.tintColor = Theme.shared.headerBg
    mealPrefsButton.isEnabled = false
}

Where theme colors are defined in a separate file like this:

static var headerBg: UIColor {
    return UIColor(red: 0.965, green: 0.969, blue: 0.973, alpha: 1.00)
}

The above is a simplified version of what I do in the app to make it clear what I am doing. I hope it would help someone trying to achieve the same. It is simple solution and works just perfectly with a few lines of code.

As an example from the app, this is how two buttons appear and disappear based on whether the My Recipes button is selected or not:

enter image description here enter image description here

Upvotes: 1

rakeshbs
rakeshbs

Reputation: 24572

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
}

Upvotes: 43

Kiran Jadhav
Kiran Jadhav

Reputation: 3317

if you want to hide/show UIBarButtonItem : For Swift 3

Used below simple code :

Declaration :

var doneButton = UIBarButtonItem()

In ViewDidLoad() or ViewWillAppear() or where you want to hide it : [hide bar button]

self.navigationItem.rightBarButtonItem = nil

where you want to show bar button : [use anywhere in your code]

self.navigationItem.rightBarButtonItem = self.doneButton
        doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.dismissPicker))

Upvotes: 2

Manikanta
Manikanta

Reputation: 3421

I have more that 2 menuitems and remove/add menuitem is an overhead. This code snippet worked for me.

func showMenuItem(){

    menuItemQuit.customView?.isHidden = false
    menuItemQuit.plainView.isHidden = false
}

func hideMenuItem(){

    menuItemQuit.customView?.isHidden = true
    menuItemQuit.plainView.isHidden = true
}

Upvotes: 3

jeff ayan
jeff ayan

Reputation: 849

Best Way is just custom your Bar buttom with image. Set barbuttom.image = nil to Hide again assign the image to show. And dont forget to make the barbutton isEnabled as false.

Upvotes: 6

Related Questions