Can we turn off the leftBarButtonItem navigation controller, which is the back button of the view controller in the iPhone?

I want to disable the default return button of the navigation controller

self.navigationItem.rightBarButtonItem.enabled = NO; 
// Below code does not work since leftBarButtonItem is always nil.
self.navigationItem.leftBarButtonItem.enabled = NO;

I did it manually as shown below. But is there a property to disable the default back button with only one line?

backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton_100.png"] forState:UIControlStateNormal];
[backButton addTarget:self  action:@selector(backAction:)  forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"  All Customers" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[buttonView addSubview:backButton];

UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];

// Now it is working.
self.navigationItem.leftBarButtonItem.enabled = NO;
+5
source share
4 answers

Using "hidesBackButton = YES" is not really an elegant solution because it is a HIDES button, which is not what we want. A valid workaround would be to add a UILabel to the window just above the back button, at least disabling the touch of the button.

Add this method to the AppDelegate class:

- (void) disableLeftBarButtonItemOnNavbar:(BOOL)disable
{
    static UILabel *l = nil;

    if (disable) {
        if (l != nil)
            return;
        l = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 160, 44)];
        l.backgroundColor = [UIColor clearColor];
        l.userInteractionEnabled = YES;
        [self.window addSubview:l];
    }
    else {
        if (l == nil)
            return;
        [l removeFromSuperview];
        [l release];
        l = nil;
    }
}

, , :

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:YES];

:

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:NO];
+5

.....

self.navigationController.navigationBar.userInteractionEnabled = NO;   //for  disabling 

self.navigationController.navigationBar.userInteractionEnabled = YES; //for again enabling
+11

Call [self.navigationItem setHidesBackButton:YES];for the view controller for which you do not want to have a back button. Then install leftBarButtonItemas usual.

+2
source

You can also use

[[_navigationController.topViewController.navigationItem leftBarButtonItem] setEnabled:NO]; // The top view controller on the stack.
[[_navigationController.visibleViewController.navigationItem leftBarButtonItem] setEnabled:NO];// Return modal view controller if it exists. Otherwise the top view controller.

you can use this when you want to disable or enable the UIViewControler from Appdelegate or any other view manager.

+1
source

All Articles