Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable navigation bar's back button

I'm trying to disable a navigation bar back button item (turning to gray and does not respond to touches).

I tried the following :

[self.navigationItem.backBarButtonItem setEnabled:NO];

Which does not work. Disabling the right button item works as a charm.

Surprisingly enough I could not find a similar question on SO. The closest one was about hiding the button (which works btw) but this is not so elegant (or adding a label to cover the button and prevent touches which keeps the same color of the back button --> also not so elegant:/).

I should mention that the view controller is a table view controller which is pushed by another navigation controller. (i.e. the back button is added automatically and not via IB or programmaticaly)

Any ideas ?

like image 355
giorashc Avatar asked Dec 29 '13 19:12

giorashc


People also ask

How do I hide the back button on my navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Full screen gestures”. The navigation bar will be hidden and you will be shown “Gesture demos” to explain how to operate the “Go back”, “Go to Home screen” and “Open Recents” functions.

How do I disable the Back button on my Iphone?

Yes, the back button can be disabled. Please navigate to Advanced Website Kiosk Settings–>Navigation–>Disable back button. Kindly enable this restriction to disallow the usage of the back button on the iOS device.

How do I hide the back button in Swiftui?

The . navigationBarBackButtonHidden(true) will hide the back button.

What is Navigationitem?

The navigation item used to represent the view controller in a parent's navigation bar.


2 Answers

You can hide using

[self.navigationItem setHidesBackButton:YES animated:YES];
like image 185
Gautam Sareriya Avatar answered Sep 29 '22 10:09

Gautam Sareriya


You do need to create a custom back button in order to disable it. Here's a simple example (No need to hide the backButtonItem):

Note that you'll probably want to disable the back swipe gesture (see here: How to disable back swipe gesture in UINavigationController on iOS 7 )

class MyClass: UIViewController {
    private var backButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        backButton = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack")

        navigationItem.leftBarButtonItem = backButton
    }

    func goBack() {
        navigationController?.popViewControllerAnimated(true)
    }

    func toggleBackButton() {
        backButton.enabled = (backButton.enabled == false) ? true : false
    }
}
like image 40
Dylan Avatar answered Sep 29 '22 12:09

Dylan