Search code examples
swiftuinavigationbaruinavigationitem

Swift NavBar Hide NavBarItem


Inside of my view controller I have a navBar with an item on the left side and right side. I also have 2 buttons inside of the view controller. I have it so each button does different functionality. I also have the swift file connected to the view.

My issue is that when I do either of the code below inside of my button actions the NavBar Items do not change. I don't know how to remove and add the item back, I also dont understand what Im doing wrong.

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

Solution

  • Connect Navigation Bar from storyboard to View Controller Class

    @IBOutlet var navBar: UINavigationBar!
    

    This will hide Button

    navBar.topItem?.rightBarButtonItem?.isEnabled = false
    navBar.topItem?.rightBarButtonItem?.tintColor = UIColor.clear
    

    This will show Button

    navBar.topItem?.rightBarButtonItem?.isEnabled = true
    navBar.topItem?.rightBarButtonItem?.tintColor = UIColor.red