开发者

Can we disabled the navigation controller of leftBarButtonItem that is back button of the view controller in iPhone?

开发者 https://www.devze.com 2023-01-06 20:52 出处:网络
I want to disabled the default back button of navigation controller self.navigationItem.rightBarButtonItem.enabled = NO;

I want to disabled the default back button of navigation controller

self.navigationItem.rightBarButtonItem.enabled = NO; 
// Below code does not work since leftBarButtonItem is always nil.
开发者_StackOverflow中文版self.navigationItem.leftBarButtonItem.enabled = NO;

I have done it with manually shown below, But Is there any property to disabled the default back button with just single 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;


Its very easy ..... just try this out

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

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


Using "hidesBackButton=YES" is really not an elegant solution, cause it HIDES the button which is not what we want. An acceptable work-around would be adding a UILabel to the window just over the back button at least disabling the touches on the button.

Add this method to your 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;
    }
}

You can call it like this from any view controller to disable:

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

To enable:

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


Call [self.navigationItem setHidesBackButton:YES]; for the view controller you do not want to have the back button. Then set the leftBarButtonItem as normal.


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 UIViewControler from Appdelegate, or any other viewcontroler.

0

精彩评论

暂无评论...
验证码 换一张
取 消