Search code examples
swiftnavigationbartabbarcontrollerrootviewcontroller

How to remove navigation bar from the first UITabBarController view?


I have an application that has a login and sign up screen and when either the 'Login' button or 'sign up' buttons are tapped they push to the tabbarcontroller. However, as soon as the tab bar loads, there is a navigation bar on the top of the first screen. If I use the bottom navigation bar to select the next screen, the navigation bar is gone and this is the same for all the screens. If I then go back to the first screen, the navigation bar on the top isn't there. I have the code written for the navigation bar to be hidden on all the views but it still shows with the back button. I have managed to remove the back button as by clicking on it, it goes back to the login and signup screen. I have tried changing the root view controller to the UITabBarController but the navigation bar on the first screen is there and when I navigate to the next screen and back, the top nav bar is gone. Been trying to remove it for a long time and cannot figure out how to do it. Apologies if it this is a simple and easy issue.


Solution

  • If you want to show navigation bar on your view controller, use your ViewWillAppear method:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    

    To hide that navigation bar after pushing another view controller, you can simply hide it:

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    

    Please leave a comment here if it works as you expected or not.