Search code examples
swiftuitabbarcontrollerback-buttonuinavigationitem

Swift 3 : I can't hide the back button


I'm using Tab Bar Controller and I have 2 view controllers on it. Before that, I have another viewController to access the tab bar. When I get the tab bar, back button always dissappears. I want to hide the back button and use another button as a left button, but I can't hide the back button.

This is the code in the viewController before the Tab Bar :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mainvc"{
        if pinLabel.text == pinPassword && pinLabel.text != ""{
            let tabBarController = segue.destination as! UITabBarController
            let destinationViewController = tabBarController.viewControllers?[0] as! ViewController

            destinationViewController.login.email = login.email
            destinationViewController.login.firstname = login.firstname
            destinationViewController.login.lastname = login.lastname
            destinationViewController.login.imageURL = login.imageURL
            destinationViewController.login.id = login.id                
        }else{

        }

    }
}

And this is the code that I'm trying to hide the back button and put another one into one of the view controller, which is the view controller that I see when I access the tab bar controller :

let leftOpenSideBarMenu: UIBarButtonItem = UIBarButtonItem(image: myimage, style: .plain, target: self, action: #selector(ViewController.openSideBarMenu))
self.navigationItem.setHidesBackButton(true, animated: false)
self.navigationItem.setLeftBarButtonItems([leftOpenSideBarMenu], animated: true)

Solution

  • Try

    self.tabBarController.navigationItem.setHidesBackButton(true, animated: false)
    self.tabBarController.navigationItem.setLeftBarButtonItems([leftOpenSideBarMenu], animated: true)
    

    instead of

    self.navigationItem.setHidesBackButton(true, animated: false)
    self.navigationItem.setLeftBarButtonItems([leftOpenSideBarMenu], animated: true)