Introduction: Why Hiding the Navigation Bar is a Pain for Beginners?
\n\nHave you ever encountered this situation: you're creating an app, the first screen should look like a full-screen landing page, without a gray bar at the top, but on the second screen the Navigation Bar is already needed for navigation? It would seem logical to just write navigationController?.isNavigationBarHidden = true in viewDidLoad. But then the magic begins: the Navigation Bar may "pop back" when returning from another screen, the animation breaks, or it doesn't hide at all.
This is one of the most popular questions on Stack Overflow (the topic "How to hide a navigation bar from first ViewController in Swift?" has thousands of views). In this tutorial, we will cover not only the basic solution but also the subtle points: how to do it elegantly, without animation bugs, and how to adapt it to modern iOS standards. If you are looking for a reliable way to hide the navigation bar on the first screen in Swift — you've come to the right place.
\n\nThe Problem: What Happens "Under the Hood"?
\n\nWhen you use UINavigationController, it manages the Navigation Bar for all child controllers. If you simply hide it in viewDidLoad of the first ViewController, the property may reset when navigating to the second screen and back. Why?
- \n
- viewDidLoad is called once — when the screen is shown again, the code won't execute. \n
- UINavigationController synchronizes the bar based on the last controller in the stack. \n
- The transition animation can "swallow" the state if you don't set the
animatedparameters. \n
But don't worry — the solution is elegant and simple. Let's write the code.
\n\nBasic Solution: Hiding the Navigation Bar in the First ViewController
\n\nThe most straightforward way is to override the viewWillAppear method. Why here? Because this method is called every time before the screen appears, unlike viewDidLoad. Thus, even after returning from another screen, the Navigation Bar will be hidden again.
Step 1: Create the First ViewController
\n\nLet's say you have a FirstViewController that is the root of a UINavigationController.
import UIKit\n\nclass FirstViewController: UIViewController {\n\n override func viewWillAppear(_ animated: Bool) {\n super.viewWillAppear(animated)\n // Hide the Navigation Bar without animation\n navigationController?.setNavigationBarHidden(true, animated: animated)\n }\n\n override func viewWillDisappear(_ animated: Bool) {\n super.viewWillDisappear(animated)\n // Show the Navigation Bar when leaving the screen\n navigationController?.setNavigationBarHidden(false, animated: animated)\n }\n}\n\nNote: we use setNavigationBarHidden(_:animated:) with the animated parameter. If you pass true, the Navigation Bar will hide/appear with an animation synchronized with the transition. This looks professional.
Step 2: Configure the Second Screen
\n\nOn the second screen (SecondViewController), the Navigation Bar will be displayed by default. No additional code is needed — it will appear automatically when navigating.
Step 3: Verify in Interface Builder or Code
\n\nMake sure your UINavigationController is set up correctly. If you are using Storyboard, check that the first screen is FirstViewController. If in code:
let navController = UINavigationController(rootViewController: FirstViewController())\n\nDone! Run the app — the first screen will be without a Navigation Bar, the second one will have it.
\n\nAdvanced Techniques: Animation and SwiftUI
\n\nThe basic solution works, but in real ap