Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide bar button item

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

like image 996
Rais Iqbal Avatar asked Aug 31 '16 23:08

Rais Iqbal


People also ask

How do I hide items in navigation bar?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How do I hide UIBarButtonItem?

There is no way to "hide" a UIBarButtonItem you must remove it from the superView and add it back when you want to display it again. This is actually not true - the method described by Max works well.


1 Answers

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.

like image 105
Riajur Rahman Avatar answered Oct 31 '22 06:10

Riajur Rahman