Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 659
grabury Avatar asked Jan 11 '15 12:01

grabury


2 Answers

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
}
like image 103
rakeshbs Avatar answered Nov 17 '22 17:11

rakeshbs


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.

like image 29
jeff ayan Avatar answered Nov 17 '22 18:11

jeff ayan