I added a navigation control to switch between views in my app. But some of the views shouldn't have 'Back' (the previous title) button. Any ideas about how to hide the back button?
15 Answers
Objective-C:
self.navigationItem.hidesBackButton = YES;
Swift:
navigationItem.hidesBackButton = true
-
@user8170 how to hide the left barbuttonitem on navigation bar..? – rockey Nov 11 '10 at 21:46
-
10you cant hide backbutton by `self.navigationItem.leftBarButtonItem = nil;`. If you have set leftbarbuttonitem explicitely you can use `self.navigationItem.leftBarButtonItem = nil;`. `self.navigationItem.hidesBackButton = YES;` will hide Back button Item – rakeshNS Aug 09 '12 at 11:28
-
1In iOS 7, self.navigationItem.leftBarButtonItem = nil; does not affect the back button when issued from the viewDidLoad, viewWillAppear, or viewDidAppear. This line of code does work within the viewdidLoad: self.navigationItem.hidesBackButton = YES; – Alex Zavatone Jan 14 '14 at 16:05
-
2not sure if this helps but 'self' is always a view controller which is being presented on the top of stack of navigation controller. – Kunal Balani Jan 17 '14 at 17:01
-
self.navigationItem.hidesBackButton = YES; works for me in viewDidLoad, ios 7 – leukosaima Jun 05 '14 at 20:48
-
self.navigationItem.hidesBackButton = yes; make issue when I use swipe gesture.which shows the backButton – Bill Xie Mar 23 '17 at 09:23
-
this is not working for me. I used this code self.navigationItem.setHidesBackButton(true, animated: true) – James Chan Oct 14 '21 at 19:01
The best way is to combine these, so it will hide the back button even if you set it up manually :
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;

- 1,768
- 1
- 16
- 17
-
7To answer user716216, he explained it in his one sentence explanation. The second line of code is what works for the default case. The first line of code works if you have created and added the button yourself. – Alex Zavatone Jan 14 '14 at 16:13
hide back button with bellow code...
[self.navigationItem setHidesBackButton:YES animated:YES];
or
[self.navigationItem setHidesBackButton:YES];
Also if you have custom UINavigationBar
then try bellow code
self.navigationItem.leftBarButtonItem = nil;

- 20,427
- 11
- 57
- 70
-
in any class when you used that code then on that class's navigationbar back button will hide ... – Paras Joshi Jan 29 '14 at 12:47
In Swift:
Add this to the controller
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: false)
}

- 7,456
- 1
- 22
- 32

- 15,628
- 6
- 82
- 76
Use the code:
self.navigationItem.backBarButtonItem=nil;

- 3,459
- 4
- 39
- 64

- 1,179
- 12
- 5
-
1That will show default back bar button item of navigation item. https://developer.apple.com/documentation/uikit/uinavigationitem/1624958-backbarbuttonitem?language=objc – Ivan Tkachenko Nov 05 '19 at 12:39
In the function viewDidLoad of the UIViewController use the code:
self.navigationItem.hidesBackButton = YES;

- 59
- 2
Don't forget that you need to call it on the object that has the nav controller. For instance, if you have nav controller pushing on a tab bar controller with a RootViewController, calling self.navigationItem.hidesBackButton = YES
on the RootViewController will do nothing. You would actually have to call self.tabBarController.navigationItem.hidesBackButton = YES

- 166
- 8
Add this code in your view controller
UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:myView];
self.navigationItem.leftBarButtonItem = btnL;

- 723
- 6
- 8
Don't forget that we have the slide to back gesture now. You probably want to remove this as well. Don't forget to enable it back again if necessary.
if ([self.navigationItem respondsToSelector:@selector(hidesBackButton)]) {
self.navigationItem.hidesBackButton = YES;
}
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- 594
- 4
- 8
-
Without this a lot of controllers without `"back button"` will be swipe-to-back enabled :) Also please note, you will need to enable back the `interactivePopGestureRezognizer` once the user leave the current scene. – dvp.petrov Jan 22 '21 at 14:24
For me none of the above seemed to work, It had no visual effect. I am using storyboards with a view that is "embedded" in a navigation controller.
I then at code level add my menuItems and for some reason the "backButton" is visible when visually debugging the view hierarchy, and my menuItem Icon is displayed beneath the invisible "back button".
I tried the settings, as suggested at the various hook methods and that had no effect. Then I tried a more brutal approach and iterate over the subview which also had no effect.
I inspected my icon sizes and appeared to be ok. After referring to he apple Human Interface Guideline I confirmed my Icons are correct. (1 pixel smaller in my case 24px 48px 72px).
The strangest part then is the actual fix...
When adding the BarButton Item give it a title with at least one character, In my case a space character.
Hopes this helps someone.
//left menu - the title must have a space
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " <--THE FIX
style:UIBarButtonItemStylePlain
target:self
action:@selector(showMenu)];
leftButtonItem.image = [UIImage imageNamed:@"ic_menu"];
[self.navigationItem setLeftBarButtonItem:leftButtonItem];

- 3,359
- 3
- 30
- 50
It wasn't working for me in all cases when I set
self.navigationItem.hidesBackButton = YES;
in viewWillAppear or ViewDidLoad, but worked perfectly when I set it in init of the viewController.

- 669
- 6
- 9
navigationItem.leftBarButtonItem = nil
navigationItem.hidesBackButton = true
if you use this code block inside didLoad or loadView worked but not worked perfectly.İf you look carefully you can see back button is hiding when your view load.Look's weird.
What is the perfect solution?
Add BarButtonItem component from componentView (Command + Shift + L) to your target viewControllers navigation bar.
Select BarButtonItem set Title = " " from right panel

- 21
- 4