Swift
iOS Development
ViewController
Navigation Bar
Programming Tutorial

How to hide a navigation bar from first ViewController in Swift?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Swift, handling the visibility of the navigation bar in a view controller is a common task. Understanding how to efficiently manage it is crucial for developing seamless user experiences in iOS applications. This article focuses on how to hide the navigation bar specifically in the first view controller of your application.

Overview

The navigation bar is typically used to manage the navigation between different views in a UIKit-based iOS app. However, there are cases where you might want the first view of your app to be free of a navigation bar for aesthetic or functional reasons, such as presenting a login screen or a splash screen.

Technical Explanation

When your application launches, it usually begins with a designated root view controller. To hide the navigation bar in this initial view controller, you need to interact with the UINavigationController that contains it.

Code Implementation

To hide the navigation bar in Swift, you would typically override certain methods in your view controller's lifecycle. Below is a step-by-step guide on how to achieve this:

Step 1: Subclass Your View Controller

If you haven’t already, create a subclass of UIViewController. This is where you’ll configure your view’s appearance.

swift
1class FirstViewController: UIViewController {
2    override func viewDidLoad() {
3        super.viewDidLoad()
4        
5        // Customize your view's appearance here.
6    }
7}

Step 2: Hide the Navigation Bar

Override the viewWillAppear lifecycle method to hide the navigation bar when the view first appears. This is critical because the navigation bar resets its visibility state when going back and forth between views.

swift
1override func viewWillAppear(_ animated: Bool) {
2    super.viewWillAppear(animated)
3    
4    // Hide the navigation bar
5    self.navigationController?.setNavigationBarHidden(true, animated: animated)
6}

Step 3: Show the Navigation Bar in Subsequent Views

When navigating away from the first view controller, you might want the navigation bar to appear in other view controllers. To handle this, use the viewWillDisappear or viewDidDisappear methods.

swift
1override func viewWillDisappear(_ animated: Bool) {
2    super.viewWillDisappear(animated)
3    
4    // Show the navigation bar for the next view controller
5    self.navigationController?.setNavigationBarHidden(false, animated: animated)
6}

Additional Considerations

  • Segue Transitions: Depending on how transitions are managed (modal vs. push), ensure that navigation bar settings don’t conflict with modal presentations. Modals typically manage their own navigation context, so the above logic is primarily applicable when using a UINavigationController.
  • Status Bar Settings: Sometimes, you might also want to manage the status bar visibility alongside the navigation bar for a truly immersive experience. Override the prefersStatusBarHidden property if desired.
swift
override var prefersStatusBarHidden: Bool {
    return true
}
  • View Controller Hierarchy: Ensure that your view hierarchy is set up correctly in the storyboard or programmatically. The FirstViewController should be embedded within a UINavigationController for the above methods to work as expected.

Summary Table

To summarize, here’s a concise table of key points for hiding the navigation bar in the first view controller:

Key PointDescription
Method to OverrideviewWillAppear(_:)
Hide Navigation Barself.navigationController?.setNavigationBarHidden(true, animated: animated)
Show for Other ViewsOverride viewWillDisappear(_:) to reset visibility
Status BarOverride prefersStatusBarHidden to hide the status bar if needed
Ensure Proper HierarchyEmbed your FirstViewController in a UINavigationController

Conclusion

Hiding the navigation bar in the initial view controller may be necessary for various application requirements. Through careful implementation of view controller lifecycle methods, you can effectively manage the navigation bar’s visibility, improving user interface consistency and enhancing user experience. Mastering these techniques arms you with greater control over your app’s navigation flow and presentation.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.