Deny
Deny

Reputation: 1461

How to disable back button in navigation bar

Is there any official way how to set UIBarButtonItem.enabled property? I tried to set a backButtonItem in previous controller. But enabled property is ignored.

More in this simple example project.

I don't want to some solution like "make your own leftBarButtonItem and set its alpha ..."

Edit: I don't want to hide it, only disable it with dimmed colour and disabled user interaction. It's exactly the same behaviour as for disabled leftBarButtonItem.

Upvotes: 51

Views: 51185

Answers (5)

kjellie
kjellie

Reputation: 382

Don't try to disable your custom back button (won't work), just set a new one which is disabled. You can reach the previous navigation item through the UINavigationBar.backItem property.

// set disabled back button
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
backButton.isEnabled = false
navigationController?.navigationBar.backItem?.backBarButtonItem = backButton

// disable pop gesture
navigationController?.interactivePopGestureRecognizer?.isEnabled = false

Upvotes: 1

Michael Schinis
Michael Schinis

Reputation: 697

I know this is an old thread, but this may help someone else. As mentioned by hennes, you can no longer disable the back button. Instead, you will need to disable the entire navigationBar.

The approach I took, was disabling the navigationBar, and then applying an 0.5 alpha to the subviews of the navigation bar.

In your ViewController class:

    func changeBarButtons(alpha: CGFloat) {
        navigationController?.navigationBar.subviews.forEach { firstViews in
            firstViews.subviews.forEach { view in
                if ["_UIButtonBarButton", "_UIButtonBarStackView"].contains(type(of: view).description()) {
                    view.alpha = alpha
                }
            }
        }
    }

    func set(loading: Bool) {
        let alpha: CGFloat = loading ? 0.5 : 1
        navigationController?.navigationBar.isUserInteractionEnabled = !loading

        changeBarButtons(alpha: alpha)
    }

Keep in mind, that Apple could change the names of the class any time. That being said, it's highly unlikely they do so. If you don't mind the title of the View Controller fading out, you can apply the alpha to all the subviews, without checking the class name.

Upvotes: 2

hennes
hennes

Reputation: 9342

As of today it is not possible to disable the back button using the enabled property. The backBarButtonItem property will be nil unless you create a custom item and even then it will ignore the enabled property. There are a couple (non-satisfactory) ways around this.

Hide the button

This is what Apple wants you to do given that they ignore the enabled property. It is as simple as

navigationItem.hidesBackButton = true  

and should be the preferred approach unless you have good reasons.

Disable and Tint the Navigation Bar

You can disable user interaction on the whole navigation bar and tint it to make the back button appear disabled.

navigationController?.navigationBar.isUserInteractionEnabled = false
navigationController?.navigationBar.tintColor = UIColor.lightGray

This does, unfortunately, affect other elements in the navigation bar as well so it might not be an option if, for instance, you have another bar button item on the right side.

Use a Custom Left Bar Button Item

The leftBarButtonItem does not ignore the enabled property so you could create a custom item and trigger the pop manually when it is activated.

navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ThisClass.backButtonTapped))
...
navigationItem.leftBarButtonItem?.isEnabled = false

func backButtonTapped() {
    self.navigationController?.popViewController(animated: true)
}

This will, however, not have the back bar button style with the leading triangular indicator.

Upvotes: 106

BKjadav
BKjadav

Reputation: 543

Add below code in your ViewController2.swift Class.

override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.hidesBackButton = true;
    }

It will hide your back button.

Upvotes: 13

Templar
Templar

Reputation: 1694

If you want to hide it, UInavigationItem has a hidesBackButton property.

Upvotes: 5

Related Questions