0

I have a UINavigationController with my AHViewController as the root view controller.

Then I tried to add a left bar button item to it from the viewDidLoad of AHViewController:

UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"grid.png"] forState:UIControlStateNormal];
    UIBarButtonItem *backButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    //[self.navigationItem setLeftBarButtonItem:backButtonBarItem];
    [self.navigationController.navigationItem setLeftBarButtonItem:backButtonBarItem];
    [backButtonBarItem release];

However, I am not seeing anything. Why is this?

2 Answers 2

1

Instead of:

[self.navigationController.navigationItem setLeftBarButtonItem:backButtonBarItem];

Do this:

[self.navigationItem setLeftBarButtonItem:backButtonBarItem];

UINavigationController uses the navigationItem of the top view controller, not its (the UINavigationController's) navigation item property.

1
  • 1
    @adit Try making a simple UIBarButtonItem using -initWithTitle:style:target:action: and see if you can get it to appear that way. If this works, the issue is some problem with either using the UIButton as the custom view of the bar button item, or they way in which you are setting up your UIButton. Apr 30, 2012 at 22:31
0

Thanks "Simon Golden" Answer Work for me.

Before: (This not show up my button)

self.navigationItem.LeftBarButtonItem: backButtonItem;

After: (This Work for me and the button show up)

[self.navigationItem setLeftBarButtonItem: backButtonItem];

If the StoryBoard didn't have LeftBarButtonItem and RightBarButtonItem in the ViewController, We have to set the Button(setLeft/RightBarButtonItem).

Otherwise just simple change the attribute of the Item (self.navigationItem.Left/RightBarButtonItem)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.