Search code examples
iosswiftswift2uinavigationbaruinavigationitem

Hide bar button items swift


How can I hide my left bar button item?

In my storyboard I dragged a Navigation Bar onto my View Controller, then a Bar Button Item. Under certain conditions I want to hide the Bar Button Item.

None of this works:

override func viewDidLoad() {
    self.navigationItem.leftBarButtonItem = nil
    self.navigationItem.leftBarButtonItems = []
    self.navigationItem.setLeftBarButtonItems([], animated: true)
}

Solution

  • I dragged a Navigation Bar onto my View Controller

    Well, don't! There is a big difference between a navigation controller interface, where you set the navigationItem, and a loosey-goosey navigation bar just sitting there in the interface, which is what you have.

    Embed your view controller in a UINavigationController and do things the right way. Then setting your navigationItem and its properties will work as expected.