iOS how to remove back button?

IosNavigationcontroller

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

You can do:

[self.navigationItem setHidesBackButton:YES];

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

Solution 2 - Ios

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.

Solution 3 - Ios

In swift

self.navigationItem.hidesBackButton = true

Solution 4 - Ios

The above code did not work for me. As suggested in https://stackoverflow.com/questions/1232557/uinavigationitem-sethidesbackbuttonyes-wont-prevent-from-going-back, I had to use:

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

Solution 5 - Ios

Try this:

[self.navigationItem setHidesBackButton:YES];

Or

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

Solution 6 - Ios

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

Solution 7 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjbeardenView Question on Stackoverflow
Solution 1 - IosPeter SarnowskiView Answer on Stackoverflow
Solution 2 - IosLJ WilsonView Answer on Stackoverflow
Solution 3 - IosHamzah MalikView Answer on Stackoverflow
Solution 4 - IosYogaView Answer on Stackoverflow
Solution 5 - IosFlaviano GomesView Answer on Stackoverflow
Solution 6 - IosCodingpanView Answer on Stackoverflow
Solution 7 - IosTai LeView Answer on Stackoverflow