[ACCEPTED]-How to hide the "back" button in UINavigationController?-uinavigationcontroller

Accepted answer
Score: 324

I just found out the answer, in a controller 1 use this:

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

And to restore it:

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

--

[UPDATE]

Swift 3.0:

self.navigationItem.setHidesBackButton(true, animated:true)
Score: 27

Add this Code

[self.navigationItem setHidesBackButton:YES];

0

Score: 22

In addition to removing the back button 6 (using the methods already recommended), don't 5 forget the user can still 'pop' to the previous 4 screen with a left-to-right swipe gesture 3 in iOS 7 and later.

To disable that (when 2 appropriate), implement the following (in 1 viewDidLoad for example):

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
     self.navigationController.interactivePopGestureRecognizer.enabled = NO;
Score: 20

Just to clarify existing answers: the hidesBackButton property 11 is the right answer, but it isn't clear 10 in many answers what self refers to. Basically 9 you should set self.navigationItem.hidesBackButton = YES in the view controller that 8 is about to get pushed (or just got pushed) onto 7 the UINavigationController.

In other words, say I have a UINavigationController named 6 myNavController. I want to put a new view on it, and when 5 I do I don't want the back button to show 4 anymore. I could do something like:

UIViewController *newVC = [[UIViewController alloc] init];
//presumably would do some stuff here to set up the new view controller
newVC.navigationItem.hidesBackButton = YES;
[myNavController pushViewController:newVC animated:YES];

When 3 the code finishes, the view controlled by 2 newVC should now be showing, and no back button 1 should be visible.

Score: 16

For hiding and showing the Back button conditionally 6 you can use following code:

-(void)viewDidAppear:(BOOL)animated
{
    if ([tempAry count]==0)
    {
        [self.navigationItem setHidesBackButton:YES animated:YES];
    }
    else
    {
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }
    [super viewDidAppear:animated];
} 

Note: in some 5 cases, you have to put it in viewDidAppear 4 method instead of viewWillAppear such cases 3 like: when you are updating array of next 2 class into previous class and then checking 1 condition into next class as above.

Score: 11

Swift iOS (I have used following)

// hide back button
        self.navigationItem.setHidesBackButton(true, animated: false)

// pgrm mark ----- ------

    // hide the back button for this view controller

    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)

        self.navigationItem.setHidesBackButton(editing, animated: animated)

    }// end setEditing

0

Score: 10

sethidesbackbutton did not work for me for 1 some reason

I used this way ->

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)]] ;
Score: 7

Always use Apple Documentation for simple 2 issues they are more straightforward and 1 lightweight :)

Here is the syntax for Swift 3.0:

self.navigationItem.setHidesBackButton(true, animated:true)

Reference

https://developer.apple.com/reference/uikit/uinavigationitem#//apple_ref/occ/instm/UINavigationItem/setHidesBackButton:animated:

Score: 6

In my case I had few issues with current 2 answers:

  • inside viewDidLoad/viewWillAppear only back icon was hidden and the string "Back" was inactive but still visible
  • inside viewDidAppear the back button disappeared...but I did not want the user to see it at all

So the solution that finally have 1 worked for me is:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self.navigationItem setHidesBackButton:YES animated:NO];
    }

    return self;
}
Score: 3

The solution suggest by Zoran Simic didn't 4 work for me for some reason.

This code did 3 work however:

MyController* controller   =   [[MyController alloc]  init];
NSArray* array             =   [[[NSArray alloc] initWithObjects:controller, nil] autorelease];

[self.navigationController setViewControllers:array animated:NO];

[controller release];

Obviously you'd have to manipulate 2 an NSArray to your taste to make it work 1 for you. Hope that helps somebody :)

Score: 1

In my UIViewController subclass I have this 1 method:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated: animated];

    // hide back button in edit mode
    [self.navigationItem setHidesBackButton:editing animated:YES];
}
Score: 0

This hides the back button and replaces 1 it with an add button in Swift:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    // This hides the back button while in editing mode, which makes room for an add item button
    self.navigationItem.setHidesBackButton(editing, animated: animated)

    if editing {
        // This adds the add item button
        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
        // Use the animated setter for the left button so that add button fades in while the back button fades out
        self.navigationItem.setLeftBarButton(addButton, animated: animated)
        self.enableBackGesture(enabled: false)
    } else {
        // This removes the add item button
        self.navigationItem.setLeftBarButton(nil, animated: animated)
        self.enableBackGesture(enabled: true)
    }
}

func enableBackGesture(enabled: Bool) {
    // In addition to removing the back button and adding the add item button while in edit mode, the user can still exit to the previous screen with a left-to-right swipe gesture in iOS 7 and later. This code disables this action while in edit mode.
    if let navigationController = self.navigationController {
        if let interactivePopGestureRecognizer = navigationController.interactivePopGestureRecognizer {
            interactivePopGestureRecognizer.isEnabled = enabled
        }
    }
}
Score: 0

Swift 3.

Generally, you should use Apple's 6 per-ViewController API as described many 5 times already on this page, but sometimes 4 you need immediate control of the Back button.

The 3 following code hides the Back button and 2 ensures that tap collision detection doesn't 1 occur in the hidden button region.

let emptyView = UIView(frame: .zero)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: emptyView)
Score: 0

This hides the back button

let backBtn = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)


navigationItem.leftBarButtonItem = backBtn

0

More Related questions