0

I'm using a tabBar controller to control three viewControllers and want to set the navigationBar hidden only on two viewControllers, but it's not working.

picture of the structure of the tabBarController and three controllers This is the current tabBarController and viewControllers. I have a viewController for my tabBar Controller, and I set a navigationBar on that viewController

class PlayerTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureNavBar()
    }
    
    func configureNavBar() {
        let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 10))
        backButton.setImage(UIImage(named: "leave 1"), for: .normal)
        backButton.addTarget(self, action: #selector(onBackPressed), for: .touchUpInside)
        let barBackButton = UIBarButtonItem(customView: backButton)
        self.navigationItem.leftBarButtonItem = barBackButton
    }

    @objc func onBackPressed() {
        self.navigationController?.popViewController(animated: true)
    }
}

TeamViewController is the screen that I want to show the navBar, so I'm not doing anything to that viewController, and I'm using this code to hide the navBar in other two controllers(timerView and rankingView)

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

The problem is that when I go to one of the two controllers, the navBar is hidden, but the navBar is visible when I move between the two screens(timer and ranking). For example, when I go to the timer or ranking screen from team screen, it works. But when I move from timer to ranking, the navBar is visible on the ranking screen and vice versa. How can I fix this problem?

nakim81
  • 1
  • 1
  • 1
    Sounds like it happens due to the call on `viewWillDisappear` when you're switching controllers. I'd suggest hiding the navigation bar permanently and including a `UIView` top bar on that specific controller instead of the nav bar as you prefer keeping it hidden otherwise – Lokesh SN Jan 11 '23 at 06:13
  • does these answer your question ? https://stackoverflow.com/questions/845583/iphone-hide-navigation-bar-only-on-first-page, https://stackoverflow.com/questions/51767122/swift-4-one-view-controller-two-views-hide-navigation-bar/51767420, https://stackoverflow.com/a/36543349/20452635 – SwiftNewbie Jan 11 '23 at 07:21
  • Sorry but can you explain in more detail about including a UIView top bar on the specific controller? – nakim81 Jan 12 '23 at 11:06

0 Answers0