50
votes

I have an application with a navigation bar that pushes to a login screen view controller and then pushes to a main menu. Is there any way I can remove the back button off the main menu, so the user is unable to go back to the login screen?

Thanks!

EDIT: Using Xcode 4.3 and doing all the leg work programmatically.

7

7 Answers

120
votes

You can do:

[self.navigationItem setHidesBackButton:YES];

In your second view controller (the one you want to hide the button in).

9
votes

Peters answer is correct, although I think the better question is why? In a schema like yours where you are wanting to login a user, instead of using a Pushed VC, present a Modal VC and use a delegate method to get back the userinfo that was obtained in the Login process. I can post a complete code example if you need it, but it sounds like you have the details worked out with your login process. Just use:

presentModalViewController

instead of:

pushViewController

That way, you don't have to worry about the navigation stack and doing something that isn't really in-line with the user interface guidelines.

7
votes

In swift

self.navigationItem.hidesBackButton = true
6
votes

The above code did not work for me. As suggested in UINavigationItem setHidesBackButton:YES won't prevent from going back, I had to use:

[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] init]]];
1
votes

Try this:

[self.navigationItem setHidesBackButton:YES];

Or

[self.navigationItem setHidesBackButton:YES animated:YES];
1
votes

Tried in Xcode7.3.1, swift

self.navigationItem.setHidesBackButton(true, animated: true)

It only hide the back arrow and disabled the back action, but I can still see the name of the previous view controller.

For those who want to also hide the name of the previous view controller, try Yoga's answer works for me. In swift

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIView())
1
votes

In the case you need to toggle show/hide the back button:

navigationItem.hidesBackButton = true/false

And keep the swipe back gesture:

extension YourViewController: UIGestureRecognizerDelegate {}

And

navigationController?.interactivePopGestureRecognizer?.isEnabled = true
navigationController?.interactivePopGestureRecognizer?.delegate = self