iPhone Navigation
Hide Back Button
iOS Development
User Interface
Mobile App Design

How to hide 'Back' button on navigation bar on iPhone?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

Customizing user interface elements is a crucial task in app development to improve user experience. In iOS app development, navigation control is one such element that often needs customization. A frequently asked question is how to hide the 'Back' button on the navigation bar for a particular view controller. This article explores the technical aspects of achieving this in iOS using Swift, focusing on different scenarios and best practices.

Understanding the Navigation Bar

The navigation bar is part of the UINavigationController in iOS. It typically contains a title, left and right bar buttons. The 'Back' button is automatically generated by the navigation controller when you push a new view controller onto the navigation stack. However, you might want to hide this button for specific view controllers due to unique design considerations or requirements.

Hiding the 'Back' Button

To hide the 'Back' button, you can use the hidesBackButton property of the UINavigationItem class. Below is a step-by-step guide on how to implement this:

Step 1: Access the View Controller

Identify the view controller in which you want to hide the 'Back' button. This is usually done within the viewDidLoad() method of your UIViewController subclass.

Step 2: Set the hidesBackButton Property

Set the hidesBackButton property to true. The hidesBackButton is a Boolean property of UINavigationItem which, when set to true, hides the 'Back' button from the navigation bar.

Example Code

Here's a snippet that illustrates how to hide the 'Back' button:

swift
1import UIKit
2
3class MyViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6        // Hides the automatic 'Back' button
7        self.navigationItem.hidesBackButton = true
8    }
9}

Advanced Customizations

Adding a Custom Left Bar Button

If you hide the 'Back' button, you might need to provide an alternative for navigation. This can be accomplished by adding a custom left bar button item.

Code Example:

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    // Hides the automatic 'Back' button
4    self.navigationItem.hidesBackButton = true
5    
6    // Add a custom left button
7    let customButton = UIBarButtonItem(title: "Home", style: .plain, target: self, action: #selector(navigateHome))
8    self.navigationItem.leftBarButtonItem = customButton
9}
10
11@objc func navigateHome() {
12    // Action to navigate home or to a specific view
13    self.navigationController?.popToRootViewController(animated: true)
14}

Conditional Hiding

There might be scenarios where you want to conditionally hide the 'Back' button based on logic or conditions.

swift
1override func viewWillAppear(_ animated: Bool) {
2    super.viewWillAppear(animated)
3    if shouldHideBackButton() {
4        self.navigationItem.hidesBackButton = true
5    }
6}
7
8func shouldHideBackButton() -> Bool {
9    // Add your condition here
10    return true  // or some condition
11}

Best Practices

  • User Experience: Always consider user experience before hiding the 'Back' button. Ensure that users have an intuitive way to navigate back if necessary.
  • Consistency: Maintain consistency across your app. Avoid hiding the 'Back' button in only some view controllers unless there's a valid reason.
  • Testing: Thoroughly test your implementation on different devices to check for any anomalies or UI issues.
  • Accessibility: After hiding the 'Back' button, make sure that the navigation is still accessible for voiceover users.

Conclusion

Hiding the 'Back' button in the navigation bar can be necessary for some app designs. Using the hidesBackButton property of UINavigationItem and optionally providing a custom navigation solution, you can manage the navigation flow effectively. Always ensure that the user experience is not compromised and consider implementing alternative navigation routes if needed.

Summary Table

MethodDescriptionCode Example
Set hidesBackButtonHides the default 'Back' button in navigation bar.self.navigationItem.hidesBackButton = true
Add Custom Left ButtonReplaces 'Back' with a custom button.UIBarButtonItem(title: "Home", style: .plain, target: self, action: #selector(navigateHome))
Conditional HidingHide the 'Back' button based on a condition.Implement logic in viewWillAppear with a conditional statement.

By following these strategies, you can effectively manage the navigation bar's appearance in your iOS applications.


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.