51

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.

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
jbearden
  • 1,869
  • 2
  • 19
  • 23

7 Answers7

121

You can do:

[self.navigationItem setHidesBackButton:YES];

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

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
9

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.

LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
8

In swift

self.navigationItem.hidesBackButton = true
Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46
6

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]]];
Community
  • 1
  • 1
Yoga
  • 1,186
  • 1
  • 13
  • 24
1

Try this:

[self.navigationItem setHidesBackButton:YES];

Or

[self.navigationItem setHidesBackButton:YES animated:YES];
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
Flaviano Gomes
  • 126
  • 2
  • 3
1

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())
Community
  • 1
  • 1
Codingpan
  • 318
  • 2
  • 6
1

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
Tai Le
  • 8,530
  • 5
  • 41
  • 34