Fix: Can’t Hide Back Button in UINavigationItem with Swift

Fix: Can't Hide Back Button in UINavigationItem with Swift

Written by

in

Quick answer: Call self.navigationItem.setHidesBackButton(true, animated: true) on the view controller being pushed, usually in viewDidLoad or viewWillAppear. If your view controller is inside a UITabBarController, set tabBarController?.navigationItem.hidesBackButton = true instead.

Why this happens

UINavigationController adds a back button automatically. It is not a button you created. When you push a second view controller onto the stack, the navigation controller creates a back button from the previous view controller’s navigationItem — using its title or its backBarButtonItem if you set one.

That means the button you see on the second screen is controlled by the second screen’s navigationItem.hidesBackButton, not the first screen’s. Setting properties on the first view controller will not hide it after the push. You have to configure the navigationItem of the view controller that is currently visible.

A common edge case is a UITabBarController embedded in a UINavigationController. In that hierarchy the visible navigation item belongs to the UITabBarController itself, not to the selected child view controller. Calling self.navigationItem.setHidesBackButton inside a tab’s child does nothing because that child’s navigationItem is not the one the navigation bar is displaying.

How to fix it

1. Hide the back button on the pushed view controller (most cases)

Do this in the view controller where you want the button hidden, not the one you are pushing from. The API is setHidesBackButton(_:animated:) on UINavigationItem.

Call it after the view has been loaded. viewDidLoad works for a static hide. viewWillAppear(_:) is better if you need the animated transition or if you show/hide conditionally when coming back from another push.

import UIKit

class DetailViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Hide without animation on first appearance
        navigationItem.setHidesBackButton(true, animated: false)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // Use animated: true if you want the hide to animate with the push
        navigationItem.setHidesBackButton(true, animated: animated)
    }
}

You can also set the property directly. These two are equivalent:

// Property form - no animation
navigationItem.hidesBackButton = true

// Method form - allows animation
navigationItem.setHidesBackButton(true, animated: true)

To show the button again when you leave, reset it:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationItem.setHidesBackButton(false, animated: animated)
}

If setHidesBackButton(true, animated: true) seems to have no effect, check that you are calling it on the correct navigationItem. Add a breakpoint and verify self.navigationController is not nil and that self.navigationItem === self.navigationController?.topViewController?.navigationItem.

2. Fix it when using a UITabBarController inside a UINavigationController

If your structure is UINavigationController -> UITabBarController -> UIViewController (tab child), the navigation bar shows tabBarController?.navigationItem. The child’s self.navigationItem is ignored for the back button.

Set hidesBackButton on the tab bar controller’s navigation item:

import UIKit

class FirstTabViewController: UIViewController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBarController?.navigationItem.hidesBackButton = true
        // or with animation:
        // tabBarController?.navigationItem.setHidesBackButton(true, animated: animated)
    }
}

If you have multiple tabs and only one should hide the button, reset it in the other tabs:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tabBarController?.navigationItem.setHidesBackButton(false, animated: animated)
}

A cleaner architecture is to avoid putting a UITabBarController inside a UINavigationController at all. Apple expects the tab bar controller to be the root. If you need navigation inside each tab, give each tab its own UINavigationController: UITabBarController -> UINavigationController -> UIViewController. Then each tab manages its own stack and self.navigationItem.setHidesBackButton works as in section 1.

3. Hide the back button but keep or replace the left button

hidesBackButton only hides the system back button. It does not affect leftBarButtonItem or leftBarButtonItems. If you set a custom left button, the back button is hidden automatically unless you explicitly keep it.

To hide the back button and show a custom left button:

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.setHidesBackButton(true, animated: false)
    navigationItem.leftBarButtonItem = UIBarButtonItem(
        title: "Close",
        style: .plain,
        target: self,
        action: #selector(closeTapped)
    )
}

@objc private func closeTapped() {
    dismiss(animated: true)
    // or navigationController?.popViewController(animated: true)
}

To hide everything on the left:

navigationItem.setHidesBackButton(true, animated: false)
navigationItem.leftBarButtonItem = nil
navigationItem.leftBarButtonItems = nil

Note: setting navigationItem.backBarButtonItem on the previous view controller changes the back button’s title/action, but does not hide it on the next screen. Hiding must happen on the pushed controller.

Hiding the back button does not disable the interactive swipe-to-go-back gesture by itself. If you want to disable that gesture when the button is hidden:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}

Only disable the gesture if you have an alternative way to leave the screen.

4. SwiftUI equivalent

If you are using SwiftUI with NavigationStack, the UIKit API does not apply. Use the SwiftUI modifier on the pushed view:

import SwiftUI

struct DetailView: View {
    var body: some View {
        Text("Detail")
            .navigationBarBackButtonHidden(true)
            // Add your own leading button if needed
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Close") { }
                }
            }
    }
}

For UIKit apps hosting SwiftUI via UIHostingController, still use navigationItem.setHidesBackButton on the hosting controller.

How to prevent it

  • Always configure hidesBackButton on the destination view controller, not the source. Do it in viewDidLoad or viewWillAppear of the screen that should not show the button.
  • Use setHidesBackButton(_:animated:) when you want the change to animate with a push/pop. Use the hidesBackButton property for an immediate, non-animated change.
  • If you embed a UITabBarController in a navigation controller, remember the navigation item belongs to the tab bar controller. Consider restructuring to UITabBarController containing UINavigationControllers per tab.
  • When you hide the back button, provide another way out (custom left button, close button, or swipe gesture) so users are not trapped.
  • Do not try to hide the button by setting backBarButtonItem to an empty item on the previous controller. That leaves a tappable blank area. Use hidesBackButton on the current controller instead.

FAQ

Why is setHidesBackButton not working?

You are likely calling it on the wrong navigationItem. It must be called on the view controller currently on top of the navigation stack. Also verify the view controller is actually inside a UINavigationController — if navigationController is nil, there is no navigation bar to hide anything from. For tab-based apps, use tabBarController?.navigationItem.setHidesBackButton.

How do I hide the back button and show a custom left button?

First call navigationItem.setHidesBackButton(true, animated: false), then assign navigationItem.leftBarButtonItem. If you assign a left item without hiding the back button, iOS may still show the back button alongside it depending on iOS version and layout. Hiding explicitly avoids that.

Does hiding the back button disable swipe to go back?

No. hidesBackButton only affects the visible button. The edge-swipe pop gesture (interactivePopGestureRecognizer) remains enabled unless you disable it yourself. If you hide the back button for a flow that should not be swiped back (like a login screen), disable the gesture in viewDidAppear and re-enable it in viewWillDisappear.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.