Preserving Navigation Bar During Programmatic Navigation in Swift

Nov 23, 2025 · Programming · 11 views · 7.8

Keywords: Swift | iOS Development | Navigation Bar | View Controller | Programmatic Navigation

Abstract: This article provides an in-depth analysis of the navigation bar disappearance issue during programmatic navigation in Swift. By comparing present and push navigation methods, it explains the root cause in iOS 13+ where the default modal presentation style is card-based, and offers solutions including setting modalPresentationStyle to fullScreen. The article covers implementations for both storyboard and programmatically created controllers with complete code examples and best practices.

Problem Background and Phenomenon Analysis

In iOS application development, programmatic navigation to another view controller is a common requirement. Developers often encounter an issue where the navigation bar unexpectedly disappears when using the presentViewController method. This phenomenon is particularly noticeable in iOS 13 and later versions, primarily due to changes in the system's default modal presentation style.

Root Cause Analysis

Starting from iOS 13, Apple introduced a new card-based modal presentation style as the default setting. This style displays a thumbnail of the previous view controller at the top of the screen and allows users to dismiss the current view controller with a swipe-down gesture. However, this design covers the original navigation bar, making navigation bar elements invisible to users.

To understand this issue, it's essential to distinguish between two main navigation approaches:

Solution Implementation

The most direct solution to the navigation bar hiding issue in modal presentation is to modify the view controller's presentation style. By setting the modalPresentationStyle property to .fullScreen, you can restore the traditional full-screen presentation mode, thereby preserving the navigation bar's visibility.

Here is the specific implementation code example:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "nextView") as! NextViewController
nextViewController.modalPresentationStyle = .fullScreen
self.present(nextViewController, animated: true, completion: nil)

Alternative Navigation Approaches

Besides modifying the presentation style, developers can also consider using the navigation controller's push functionality. This method naturally preserves the navigation bar and provides a standard back navigation experience.

Push implementation for programmatically created controllers:

let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)

Push implementation for storyboard controllers:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.navigationController?.pushViewController(newViewController, animated: true)

Style Selection Considerations

When choosing presentation styles, developers should consider the following factors:

Best Practice Recommendations

Based on practical development experience, we recommend following these best practices:

  1. Prioritize using the pushViewController method when full navigation bar functionality is required
  2. When modal presentation is necessary, choose the appropriate presentation style based on the target iOS version
  3. For applications needing to support multiple iOS versions, implement version detection and conditional settings
  4. Establish unified navigation implementation standards in team development to ensure code consistency

Compatibility Considerations

It's important to note that the .fullScreen presentation style was the default setting in versions prior to iOS 13. Therefore, setting this property won't have negative effects on older systems. This backward compatibility makes the solution suitable for applications that need to support multiple iOS versions.

By appropriately selecting navigation methods and presentation styles, developers can ensure that applications provide consistent and user-friendly navigation experiences across different iOS versions while maintaining complete navigation bar functionality.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.