4

I have a navigation toolbar in which I am adding toolbar items programatically, as below. The toolbar displays properly, and the toolbar style is set to black opaque. but the button on the toolbar does not display. Why?

    //Set up the toolbar
 [[[self navigationController] toolbar] setBarStyle:UIBarStyleBlackOpaque]; 
 UIBarButtonItem *myButtonItem = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(handleMyButton)];
 NSArray *myItems = [NSArray arrayWithObjects: myButtonItem,nil];
 [[self navigationController] setToolbarItems:myItems animated:NO];
 [myButtonItem release];

4 Answers 4

26

UINavigationController fetches the buttons that should be used for the navigation bar and the tool bar from the current visible view controller. This means that you add the buttons you want to have to the view controller, not the navigation controller. So it should work just fine if you do:

[self setToolbarItems:myItems animated:NO];

Compare that with how the add button is added to the navigation bar in the default template for a Navigation Based Application with Core Data:

self.navigationItem.rightBarButtonItem = addButton;

This means that when you push a new view controller the buttons in the tool bar will disappear and then appear again when you pop back.

3
  • 1
    Robert - Thanks. That not only solves the issue, but helps me understand what's going on.
    – Don Wilson
    Nov 28, 2010 at 20:34
  • Robert, great answer! I have to agree with Don Wilson! Thanks
    – mobius
    Mar 11, 2011 at 14:06
  • thanks a lot for [self setToolbarItems:myItems animated:NO];
    – Massmaker
    Sep 3, 2015 at 9:34
0

You are referencing the toolbar owned by your navigationController in the first line and not in the 4th line. It would appear that the necessary fix is:

[[[self navigationController] toolbar] setToolbarItems:myItems animated:NO];

instead of your current line 4.

2
  • UIToolbar does not have a setToolbarItems method. It does have a setItems method, as described in the answer below. However, if I use that approach, I get the same result. UINavigationController has a setToolbarItems method whose purpose is to set items on the default toolbar, correct? I'm still not seeing why the code above has a problem.
    – Don Wilson
    Nov 28, 2010 at 18:30
  • This line is more coorect [[[self navigationController] toolbar] setItems:items animated:NO]; Nov 19, 2012 at 7:44
0

Show the toolbar by setting the toolbarHidden property of the navigation controller object to NO.

0

To assign buttons to the toolbar you would call this method

[toolbar setItems:];

Instead of

[[self navigationController] setToolbarItems: animated:];
1
  • Tried this, with same result. See comments above.
    – Don Wilson
    Nov 28, 2010 at 18:31

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.