Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to hide the backBarButtonItem than this?

I have a way of hiding the back button used by the navigation controller. It's set by the previous controller, not the one managing the current view, and that makes it tricky to get to. I needed to do this in editing mode so that I could prevent the user from navigating away from the screen.

if(self.editing) {
    // Get rid of the back button   
    UIView *emptyView = [[UIView alloc] init];;
    UIBarButtonItem *emptyButton = [[[UIBarButtonItem alloc] initWithCustomView:emptyView] autorelease];
    [self.navigationItem setLeftBarButtonItem:emptyButton animated:YES];
} else {
    // Restore the back button
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];        
}

Is there a better way to do this?

like image 310
Steve Weller Avatar asked Mar 27 '09 16:03

Steve Weller


People also ask

How to hide the navigation back button in iOS swift?

Go to attributes inspector and uncheck show Navigation Bar to hide back button.

How to hide back button on navigation bar in iOS?

To hide the back button on navigation bar we'll have to either set the navigation button as nil and then hide it or hide it directly.


3 Answers

Swift5:

self.navigationItem.setHidesBackButton(true, animated: false)
like image 95
Mohsen Sedaghat Fard Avatar answered Nov 10 '22 15:11

Mohsen Sedaghat Fard


use this to hide back button

[self.navigationItem setHidesBackButton:YES]

use this to show back button

[self.navigationItem setHidesBackButton:NO]
like image 36
Raj Avatar answered Nov 10 '22 16:11

Raj


Here's the method I use in my view controller to show and hide the back button when editing is enabled and disabled:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    if (editing) {
        // Disable the back button
        [self.navigationItem setHidesBackButton:YES animated:YES];
    }
    else {
        // Enable the back button
        [self.navigationItem setHidesBackButton:NO animated:YES];
    }

    [super setEditing:editing animated:animated];
}
like image 22
nevan king Avatar answered Nov 10 '22 17:11

nevan king