iOS how to remove back button?
Asked Answered
B

7

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.

Balmuth answered 29/2, 2012 at 12:46 Comment(0)
M
121

You can do:

[self.navigationItem setHidesBackButton:YES];

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

Medeah answered 29/2, 2012 at 12:50 Comment(3)
Might want to re-think using the navigation stack and instead present a modal VC - see below.Grow
For future reference, if it isn't working for you just put that code in viewDidAppear: and it will start working.Cowry
self.navigationItem.hidesBackButton = true for Swift 4+Lithophyte
G
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.

Grow answered 29/2, 2012 at 13:6 Comment(0)
T
8

In swift

self.navigationItem.hidesBackButton = true
Tehuantepec answered 30/10, 2015 at 14:35 Comment(1)
It works, but the swipe back gesture gonna be lost!Hornstone
S
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]]];
Stringpiece answered 3/2, 2013 at 17:19 Comment(0)
F
1

Try this:

[self.navigationItem setHidesBackButton:YES];

Or

[self.navigationItem setHidesBackButton:YES animated:YES];
Forwent answered 3/12, 2013 at 15:54 Comment(0)
B
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())
Blucher answered 22/9, 2016 at 16:34 Comment(0)
H
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
Hornstone answered 11/11, 2020 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.