How to Hide Navigation Bar on the First Screen in Swift: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Hiding the Navigation Bar is a Pain for Beginners?

\n\n

Have 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.

\n\n

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\n

The Problem: What Happens "Under the Hood"?

\n\n

When 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\n
    \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 animated parameters.
  • \n
\n\n

But don't worry — the solution is elegant and simple. Let's write the code.

\n\n

Basic Solution: Hiding the Navigation Bar in the First ViewController

\n\n

The 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.

\n\n

Step 1: Create the First ViewController

\n\n

Let's say you have a FirstViewController that is the root of a UINavigationController.

\n\n
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\n

Note: 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.

\n\n

Step 2: Configure the Second Screen

\n\n

On the second screen (SecondViewController), the Navigation Bar will be displayed by default. No additional code is needed — it will appear automatically when navigating.

\n\n

Step 3: Verify in Interface Builder or Code

\n\n

Make sure your UINavigationController is set up correctly. If you are using Storyboard, check that the first screen is FirstViewController. If in code:

\n\n
let navController = UINavigationController(rootViewController: FirstViewController())
\n\n

Done! Run the app — the first screen will be without a Navigation Bar, the second one will have it.

\n\n

Advanced Techniques: Animation and SwiftUI

\n\n

The basic solution works, but in real ap

Blogs

Book Recommendations