8

My screen flow is as follow

Login -> Registration -> CarDetails

All 3 above screens are in navigation-controller, but users are not allowed to go back from Car-Details to Registration. To achieve it, I've

override func viewWillAppear(animated: Bool)
    {
        self.navigationItem.setHidesBackButton(true, animated:true);
    } 

in CarDetails view controller. So it hides the back button which automatically gets created if controller is in navigation-controller.

So far it is good.

After providing all detail user lands on Home Screen where I've slide out menu. From menu user can goto to CarDetail Screen as well (to update). That time instead of backButton I need slide-out menu button as left bar button. So I've created it using storyboard.

The problem is that it is getting displayed after Registration view as well. I need conditional show/hide functionality for it in Car-Details View.

I've hook for it as well, as following

override func viewDidLoad()
{
    super.viewDidLoad()


    if menuButtonVisibility
    {

        if self.revealViewController() != nil
        {
            menuButton.target = self.revealViewController()
            menuButton.action = "revealToggle:"
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    }

    else
    {

    }

    menuButtonVisibility=true
}

I only need the line to put in else block.

enter image description here

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
AbaEesa
  • 998
  • 1
  • 6
  • 22
  • Possible duplicate of [How to hide a bar button item for certain users](http://stackoverflow.com/questions/27887218/how-to-hide-a-bar-button-item-for-certain-users) – Santiago Carmona González Sep 01 '16 at 00:40
  • What is getting displayed? "_The problem is that it is getting displayed after Registration view as well._" – Bista Sep 01 '16 at 04:25

5 Answers5

15

You can hide it by disable the button & change it tintColor like that,

self.navigationItem.rightBarButtonItem?.isEnabled = false
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clear

do it right or left BarButtonItem whatever you preferred. Hope it helps.

Nikunj Damani
  • 753
  • 3
  • 11
Riajur Rahman
  • 1,976
  • 19
  • 28
6

you can hide it like this:

self.navigationItem.leftBarButtonItem = nil

and you can add a new left button like this:

self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Button", style: .Plain, target: self, action: #selector(ViewController.buttonTapped))

I hope that helps,

Cheers

  • In that situation, I'll need to remove & add bar button. I know that way I can achieve it. But am searching something better. Thanks by the way. – AbaEesa Sep 01 '16 at 00:40
  • Try my answer for the rightBarButtonItem and see if it works with leftBarButtonItem: http://stackoverflow.com/questions/3042818/hide-the-rightbarbuttonitem-of-a-navigation-controller/22617176#22617176 – Mike Taverne Sep 01 '16 at 03:14
  • Abu Eesa Something better means? – user3182143 Sep 01 '16 at 05:04
  • @AbuEesa I don't know what you mean with "something better." good luck. – Pedro Peres Sep 08 '16 at 00:00
4

Swift 3.1 | Works great to hide my rightBarButton

  // viewDidLoad 
  self.rightBarButtonAlert.isEnabled = false
  self.rightBarButtonAlert.tintColor = .clear

Before:

enter image description here

After:

enter image description here

Brian Bird
  • 1,176
  • 18
  • 21
1
  for (UIBarButtonItem *item in self.navigationItem.rightBarButtonItems) {
        item.width = -50;
        item.enabled = false;
    }
Giang
  • 2,384
  • 2
  • 25
  • 26
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/13534705) – Sanoj Kashyap Sep 02 '16 at 09:10
  • i checked my answer and i tested this problem , so that i post anwer.... why do you think my anser not provide to the question? – Giang Sep 02 '16 at 09:13
  • question is asking about hiding the element where your answer does not hide it just making disable. make sense? – Sanoj Kashyap Sep 02 '16 at 09:15
  • Dont thinks your reputation more than me.. you can speak ridiculous things – Giang Sep 02 '16 at 09:16
  • ah but uibarbtn is not hidden by set left/right navigationhidden... this is another way to hidden bar btn – Giang Sep 02 '16 at 09:18
  • if you dont try it... you can not hurry such conclusions – Giang Sep 02 '16 at 09:20
  • Make sense to me, I will check and will comment again. – Sanoj Kashyap Sep 02 '16 at 09:21
0

To hide barButtonItem in swift

barbuttonItem1.isEnabled = false barbuttonItem1.tintColor = UIColor.clear

dobiho
  • 1,025
  • 1
  • 11
  • 22