Swift
iOS Development
Navigation Bar
ViewController
SwiftUI

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

Introduction

In iOS app development using Swift, developers might need to tailor the user interface to meet specific design requirements. One common requirement is to hide the navigation bar on the first ViewController to enhance the visual presentation or to emphasize certain design elements. This needs to be executed while maintaining smooth navigation throughout the app. This article explains how to achieve this using practical methods and examples.

Overview of UINavigationController and Navigation Bar

The UINavigationController class manages a stack of view controllers to provide a drill-down interface for hierarchical content. It customarily includes a navigation bar at the top of the view which is used for navigation through different view hierarchy levels. The navigation bar displays a back button by default and can hold additional items like titles and action buttons.

Hide Navigation Bar on First ViewController

To hide the navigation bar on the first ViewController, several strategies can be employed. This is important when you want to present a different visual for the opening screen, such as a full-screen welcome, login, or introductory page.

Method 1: Hide in ViewWillAppear

One straightforward method to hide the navigation bar is by using the viewWillAppear() lifecycle method inside your first ViewController. Here's how you can do it:

swift
1override func viewWillAppear(_ animated: Bool) {
2    super.viewWillAppear(animated)
3    self.navigationController?.setNavigationBarHidden(true, animated: animated)
4}

In this snippet, setNavigationBarHidden(_:animated:) is called with true, which hides the navigation bar for the current view controller before it appears on the screen.

Method 2: Hide in ViewDidLoad

Alternatively, you can hide the navigation bar when the view controller is initially loaded. This method is effective for ensuring that no momentary flicker of the bar occurs:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    self.navigationController?.setNavigationBarHidden(true, animated: false)
4}

Using this method can be particularly effective if performance during the initial view load needs optimization.

Restoring the Navigation Bar in Subsequent ViewControllers

To restore the navigation bar when transitioning to another ViewController, you should unhide it in the viewWillAppear() of the next controller:

swift
1override func viewWillAppear(_ animated: Bool) {
2    super.viewWillAppear(animated)
3    self.navigationController?.setNavigationBarHidden(false, animated: animated)
4}

This code ensures that once you navigate away from the first ViewController, the navigation bar will reappear as expected on subsequent views in the hierarchy.

Considerations

  • Animated Transitions: When hiding or showing the navigation bar, consider using the animated option to ensure smooth transitions.
  • Back Button: Hiding the navigation bar removes the default back button, so navigation requires other elements, such as custom buttons, gestures, or programmatic navigation.
  • Orientation and Layout: Ensure that the content layout is correctly adjusted when hiding the navigation bar to prevent issues in different screen sizes and orientations.

Example Use Case: Login Screen

Consider an app with an initial login screen where the navigation bar must be hidden. As users authenticate and transition to the primary content view, the navigation bar should display as part of normal navigation operations.

Summary Table

Below is a table summarizing the methods to hide the navigation bar and their implications:

MethodAdvantagesUse Case
viewWillAppearEnsures consistent UIWhen consistent appearance is necessary.
viewDidLoadOptimizes load timeWhen loading performance is a priority.
Unhiding in Subsequent ViewsFacilitates navigationReinstate navigation, retain app flow.

Conclusion

The ability to hide the navigation bar in a ViewController can be straightforward but requires careful consideration of navigation and UX design principles. By leveraging lifecycle methods in Swift, developers can effectively tailor the navigation bar behavior to meet the design and functionality needs of an iOS application.


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.