15

I have tried to hide the back button in my navigation controller by adding the following lines to my viewDidLoad method. Many other previous answers on SO have said this works, so I don't know what my problem is? Maybe something new with iOS 7 / Xcode 5?

self.navigationItem.hidesBackButton = YES;
self.navigationController.navigationItem.hidesBackButton = YES;
pkamb
  • 33,281
  • 23
  • 160
  • 191
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • 3
    Are self.navigationItem and self.navigationController.navigationItem returning non-nil objects? – Cutetare Nov 19 '13 at 05:15
  • NO this will also work with iOS7/Xcode5... May be issue with your Stack of Project designed... – Kumar KL Nov 19 '13 at 05:17
  • @Cutetare how do I determine if they are returning non-nil objects? I set a break point on the lines, but navigationController does not show up under self in debug console window. Also I should add that I created this view controller in storyboard, so will that cause this code to not work? – Adam Johns Nov 22 '13 at 05:09
  • @KumarKl could it be because I created the view controller using storyboards? – Adam Johns Nov 22 '13 at 05:10
  • You can either simply `NSLog(@"%@", self.navigationItem)` or set a breakpoint, and in the debug console (the same part where the NSLogs are kept, not the left part), when your breakpoint is reached, and the console is asking (lldb), just type `po self.navigationItem`. This should work even if you are using storyboard (assuming you have connected your objects appropriatly). – Cutetare Nov 22 '13 at 05:14

4 Answers4

36

I too had faced similar issue. This will work only when you have not customized your Navigation bar. Either one of the below one will work.

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

Please check whether your getting "Back" in iOS 7, then the above will work.

Hope this will help you to identify the issue

Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
Nattudurai
  • 856
  • 7
  • 9
7

In UINavigationController we can hide Like this:

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

And In UITabBarController we can Hide Like this:

self.tabBarController.navigationItem.hidesBackButton = YES;
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Nag Raj
  • 880
  • 1
  • 12
  • 18
4
self.navigationItem.leftBarButtonItem = 
 [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] init]];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
JPlay
  • 61
  • 2
  • 1
    Please add some explaination why/how this code works and why the OPs code doesn't if you can – KhorneHoly Jul 21 '14 at 07:12
  • 1
    I just create a empty leftBarButton to hide backButton.And OPs code is work ok .I think he has another bug. – JPlay Jul 22 '14 at 03:13
  • I think it would be more helpful for the OP and further visitors, when you add some explaination to your intension. – Reporter Aug 20 '14 at 11:36
  • this is the only way i've found to correctly hide the back button from every caller piece of code – Gianluca P. Dec 11 '14 at 12:47
2

Use following code :

[self.navigationItem setHidesBackButton:YES animated:YES]; // hide back button

[self.navigationItem setBackBarButtonItem:nil]; // set as nil

[self.navigationItem setLeftBarButtonItem:nil animated:NO];  // left bar item as nil
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45