Reputation: 2303
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];
Upvotes: 4
Views: 5438
Reputation: 300
To assign buttons to the toolbar you would call this method
[toolbar setItems:];
Instead of
[[self navigationController] setToolbarItems: animated:];
Upvotes: 0
Reputation: 443
Show the toolbar by setting the toolbarHidden property of the navigation controller object to NO.
Upvotes: 0
Reputation: 10070
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.
Upvotes: 26
Reputation: 11465
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.
Upvotes: 0