jbearden
jbearden

Reputation: 1869

iOS how to remove back button?

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.

Upvotes: 51

Views: 30465

Answers (7)

Tai Le
Tai Le

Reputation: 9266

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

Upvotes: 1

Codingpan
Codingpan

Reputation: 318

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())

Upvotes: 1

Flaviano Gomes
Flaviano Gomes

Reputation: 126

Try this:

[self.navigationItem setHidesBackButton:YES];

Or

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

Upvotes: 1

Hamzah Malik
Hamzah Malik

Reputation: 2570

In swift

self.navigationItem.hidesBackButton = true

Upvotes: 8

Yoga
Yoga

Reputation: 1196

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]]];

Upvotes: 6

LJ Wilson
LJ Wilson

Reputation: 14427

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.

Upvotes: 9

Peter Sarnowski
Peter Sarnowski

Reputation: 11970

You can do:

[self.navigationItem setHidesBackButton:YES];

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

Upvotes: 121

Related Questions