iOS
UINavigationController
back button
custom text
Swift programming

UINavigationController back button custom text?

Interview Questions practice on Codemia

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

Browse interview questions

The UINavigationController in iOS is a fundamental component of UI development, providing a seamless navigation experience by managing a stack of view controllers. A ubiquitous part of this controller is the "back button," which allows users to return to the previous view controller in the stack. While developers often use the default "Back" text, there are scenarios where customizing this text is both necessary and beneficial for a better user experience.

Customizing the Back Button Text

Default Behavior

By default, the "back button" of a UINavigationController displays the title of the previous view controller. For instance, if a user navigates from a "Home" view to a "Settings" view, the back button in the "Settings" view would automatically show "Home."

Use Cases for Custom Text

Customizing the back button text can be useful in various scenarios:

  1. Localization: You may want to provide translations for the button in different languages.
  2. Space Constraints: If the default title is too long, a shorter, concise version can improve aesthetics and usability.
  3. Contextual Information: Sometimes, a nondescript label like "Back" might provide a clearer navigation cue.

Technical Implementation

To customize the back button text:

  1. Set Shorter Text on Previous View Controller: Set the navigationItem.backBarButtonItem of the previous view controller, not the current one. This ensures the modified text appears on the next page when navigating back.
  • Consistent User Experience: Ensure the custom text still maintains clarity and does not confuse users.
  • No Direct Title Change: Changing self.title in the view controller does not affect the back button text.
  • Limitations: Complex customizations, like adding images to the text or significant layout changes, might require a custom navigation item setup.
  • Using Custom Views: If further customization is required, you can create a custom view for the back button but ensure it mimics the default button's behavior for consistency with user expectations.
  • Subclassing UINavigationBar: For deeper customization needs, you might consider subclassing UINavigationBar to override its drawing or layout behavior. This is a complex task and is generally recommended only if absolutely necessary.

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.