Reputation: 1869
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
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
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
Reputation: 126
Try this:
[self.navigationItem setHidesBackButton:YES];
Or
[self.navigationItem setHidesBackButton:YES animated:YES];
Upvotes: 1
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
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
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