In-depth Analysis and Implementation of Custom Back Button Text in iOS Navigation Controller

Nov 23, 2025 · Programming · 7 views · 7.8

Keywords: iOS | UINavigationController | Back Button Customization

Abstract: This article provides a comprehensive analysis of customizing back button text in iOS UINavigationController. By examining the navigation mechanism of UIKit framework, it explains the design principle that the back button belongs to the previous view controller, and presents two implementation approaches: setting backBarButtonItem in prepareForSegue and viewDidLoad/viewWillAppear. The article also compares code implementation with Interface Builder configuration, helping developers understand best practices for different scenarios.

Analysis of Back Button Ownership Mechanism in Navigation Controller

In iOS development, customizing the back button text in UINavigationController is a common but often misunderstood requirement. The key insight is understanding that the back button actually belongs to the previous view controller, not the currently displayed view controller. This design stems from UIKit's navigation stack model, where clicking the back button returns to the previous controller in the navigation stack.

Common Mistaken Attempts and Root Cause Analysis

Many developers attempt to modify the back button text in the current view controller, but the following code snippets all fail:

self.navigationItem.backBarButtonItem?.title = "Back"
self.backItem?.title = ""
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationItem.backBarButtonItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title="Back"
self.navigationController?.navigationBar.backItem?.title = ""
self.navigationController?.navigationItem.backBarButtonItem?.title

These attempts fail because they operate on the wrong object or at the wrong timing. Back button configuration must be completed before navigation occurs and in the source view controller.

Correct Implementation Approaches

Approach 1: Configuration in prepareForSegue Method

This is the most recommended approach for navigation using Storyboard Segues. Override the prepareForSegue method in the source view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = "Something Else"
    navigationItem.backBarButtonItem = backItem
}

This code creates a new UIBarButtonItem instance, sets its title to the custom text, and assigns it to the navigationItem's backBarButtonItem property. The configured back button will appear in the next view controller being pushed.

Approach 2: Configuration in viewDidLoad or viewWillAppear

For programmatic navigation without using Segues, configure in the view controller's lifecycle methods:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.backBarButtonItem = UIBarButtonItem(
        title: "Something Else", style: .plain, target: nil, action: nil)
}

This method directly initializes UIBarButtonItem with specified style and title, suitable for any scenario requiring custom back button text.

Interface Builder Configuration Approach

Beyond code implementation, configuration can also be done through Interface Builder:

Select the Navigation Item of the source view controller in Storyboard, and set the Back Button property to the desired text in Attributes Inspector. This approach is suitable for simple text modifications, but code implementation offers more flexibility for complex dynamic configurations.

Deep Technical Principle Analysis

UINavigationController maintains a navigation stack where each view controller corresponds to a UINavigationItem. When pushing a new controller, the system automatically uses the previous controller's backBarButtonItem to create the back button. If backBarButtonItem is nil, the system uses the default "Back" text or the previous controller's title.

Best Practice Recommendations

1. Configure in prepareForSegue for Segue-based navigation, ensuring setup completes before transition
2. Configure in viewDidLoad for programmatic navigation, ensuring one-time configuration
3. Avoid modifying back button in current view controller, as this violates UIKit design principles
4. Consider user experience - custom text should be concise and consistent with app design language

Compatibility Considerations

The aforementioned approaches work in Swift 3, 4, 5 and later versions. For Objective-C projects, the implementation logic remains the same with only syntax differences. Choose the most suitable implementation based on specific navigation requirements and code architecture in actual projects.

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.